Create a new language (3)--Add a semantic processing program

Source: Internet
Author: User

Long time no update, mainly is the study of C + + object-oriented design approach and better architecture of the idea of the program. In fact, this part of the code is already completed today, but I hope we can better explain how to make a new programming language available.

Last time we talked about the semantic analysis function of bison, but did not add the corresponding semantic processing function, we construct an abstract syntax tree to describe the syntax.

First of all, the semantic processing function of bison is very convenient, as long as the following add C + + Semantic action code can be, but note that it $$ $1 $2 $3 is an internal variable, the sum of the $$ elements, the rest of the expression of the elements in the production, (the script is also among them $ ).

What these elements are specifically variables is determined by the name of the element variable we defined earlier.

So our processor turns out to look like this:

/ * PARSER.Y * /%{#include <stdio.h> #include"State.h"externintYylex (); externintYylineno;extern char* Yytext;externintYyleng;extern state* root;void yyerror (const char*s);%}%union{State*s; Char*str= NULL;}%token<str> ID STRING SCRIPT%type<s> list item Bnf_item bnf_list symbol_list symbol name%startList%%/* General List of mixed BNF and scripts */List:item {$$= new State ();$$->addchildrenstate ( $); Root =$$; }     | List item { $->addchildrenstate ( $);$$= $; }     ;/* Can be BNF or script */Item:bnf_item {$$= $; } | SCRIPT {$$= new State ();$$->state_type = script;$$->script = $; }     ;/ * The definition of a line BNF * /Bnf_item:symbol' = 'Bnf_list'; '{ $->addchildrenstate ( $);$$= $;$$->islist = false; }         ;/ * section behind the BNF * /bnf_list:symbol_list {$$= $; } | Bnf_list' | 'symbol_list { $->addbrotherstate ( $);$$= $; }         ;/ * A list of BNF items * /Symbol_list:symbol {$$= new State ();$$->state_type = temporality;$$->addchildrenstate ( $); }| Symbol_list symbol { $->addchildrenstate ( $);$$= $; }            ;/ * BNF symbol available * /Symbol:' < 'Name' > '{$$= $;$$->state_type = statement; }       |' ['Name'] '{$$= $;$$->state_type = terminal; }       |' E '{$$= new State ();$$->state_type = Epsilon; }       | STRING {$$= new State ();$$->state_type = constant;$$->state_const = $; }   | SCRIPT {$$= new State ();$$->state_type = script;$$->script = $; }       ;/ * Name, and you can define the instance name * /Name:id {$$= new State ();$$->state_class = $; }     | Id': 'ID {$$= new State ();$$->state_class = $;$$->state_var = $; }     ;%%void Yyerror (const char* s) {fprintf (stderr,'%s \ n ', s); fprintf (stderr,"line%d:", Yylineno); fprintf (stderr,"error%s \ n", Yytext);}

%type <s>is to bind the current node to the corresponding member name, based on the element name definition in the union.

These action code is very important, but also the idea is very clear, is to turn the current logical structure into an abstract syntax tree, save it, for our next operation to use.
After that we only need to parse this AST to know the grammatical structure of our BNF grammar.

We do not currently have a very surprising conclusion, but we are writing a reliable compiler, the lexical parser, the parser will be written by us, and these code, is the parsing parser configuration syntax of the first step.

Not too impatient, because, want to implement a usable LALR parser, the workload is still large, but I still hope, can be the most important part of the share to everyone.

So below, let's parse the generated AST and extract the BNF paradigm.

This is a Bnfparser class:

/ * * @Author: sxf* @Date: 2015-04-17 10:05:26* @Last Modified by:sxf* @Last Modified time:2015-04-17 11:06:18*/< /c2>#ifndef Bnf_parser_h#define BNF_PARSER_H#include "afx.h"#include "State.h" class bnfparser { Public: state* Analysis (Const Char* filename);//For debug    voidPrinttree ();Private: state* state_root;//For debug    voidPrintnode (state* S,intd);};#endif//Bnf_parser_h
/ * * @Author: sxf* @Date: 2015-04-17 10:30:02* @Last Modified by:sxf* @Last Modified time:2015-04-17 14:06:04*/< /c0>#include "BNFParser.h"#include "PARSER.HPP"#include <Stdio.H>extern FILE*Yyin; State*Root= NULL; State*Bnfparser:: Analysis(const char*FileName) {/* Open the file and change the yyin stream. * /FILE*file_in;if((file_in=fopen (filename,"R"))==NULL) {printf ("error on open%s file!", filename); GetChar ();return NULL; } Yyin=file_in;    Yyparse (); State_root=Root/ * You should close the file. * /Fclose (file_in);returnRoot;}voidBnfparser::p rintnode(state*S,int d) {if(s== NULL)return; for (int i= 0; I<D++i) printf ("    ");if(s -State_type==Statement||S -State_type==Terminal) printf ('%s '%s ', S -State_class,s -State_var);if(s -State_type==Constant) printf ('%s ', S -State_const);if(s -State_type==temporality) printf ("Temp Node");if(!S -Islist) printf ("(not list)");if(s -Repeatable>0)    {if(s -Repeatable== 1) printf (" ?");if(s -Repeatable== 2) printf (" +");if(s -Repeatable== 3) printf (" *"); } printf ("\ n"); Printnode (S -Children,d+1); Printnode (S -BROTHER,D);}voidBnfparser::p rinttree() {Printnode (State_root,0);}

In this way, write a main function, you can test our code is correct, we use the following code to generate a test to see

    bison -o Parser.cpp parser.y

Create a new language (3)--Add a semantic processing program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.