English 中文(简体)
How to turn this into a parser
原标题:
  • 时间:2009-11-17 09:35:29
  •  标签:
  • parsing
  • yacc

If I just add on to the following yacc file, will it turn into a parser?

/* C-Minus BNF Grammar */

%token ELSE
%token IF
%token INT
%token RETURN
%token VOID
%token WHILE

%token ID
%token NUM

%token LTE
%token GTE
%token EQUAL
%token NOTEQUAL
%%

program : declaration_list ;

declaration_list : declaration_list declaration | declaration ;

declaration : var_declaration | fun_declaration ;

var_declaration : type_specifier ID  ; 
                | type_specifier ID  [  NUM  ]   ;  ;

type_specifier : INT | VOID ;

fun_declaration : type_specifier ID  (  params  )  compound_stmt ;

params : param_list | VOID ;

param_list : param_list  ,  param
           | param ;

param : type_specifier ID | type_specifier ID  [   ]  ;

compound_stmt :  {  local_declarations statement_list  }  ;

local_declarations : local_declarations var_declaration
                   | /* empty */ ;

statement_list : statement_list statement
               | /* empty */ ;

statement : expression_stmt
          | compound_stmt
          | selection_stmt
          | iteration_stmt
          | return_stmt ;

expression_stmt : expression  ; 
                |  ;  ;

selection_stmt : IF  (  expression  )  statement
               | IF  (  expression  )  statement ELSE statement ;

iteration_stmt : WHILE  (  expression  )  statement ;

return_stmt : RETURN  ;  | RETURN expression  ;  ;

expression : var  =  expression | simple_expression ;

var : ID | ID  [  expression  ]  ;

simple_expression : additive_expression relop additive_expression
                  | additive_expression ;

relop : LTE |  <  |  >  | GTE | EQUAL | NOTEQUAL ;

additive_expression : additive_expression addop term | term ;

addop :  +  |  -  ;

term : term mulop factor | factor ;

mulop :  *  |  /  ;

factor :  (  expression  )  | var | call | NUM ;

call : ID  (  args  )  ;

args : arg_list | /* empty */ ;

arg_list : arg_list  ,  expression | expression ;
问题回答

Heh
Its only a grammer of PL
To make it a parser you need to add some code into this.
Like there http://dinosaur.compilertools.net/yacc/index.html Look at chapter 2. Actions
Also you d need lexical analyzer -- 3: Lexical Analysis





相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

How to get instance from string in C#?

Is it possible to get the property of a class from string and then set a value? Example: string s = "label1.text"; string value = "new value"; label1.text = value; <--and some code that makes ...

XML DOM parsing br tag

I need to parse a xml string to obtain the xml DOM, the problem I m facing is with the self closing html tag like <br /> giving me the error of Tag mismatch expected </br>. I m aware this ...

Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

Locating specific string and capturing data following it

I built a site a long time ago and now I want to place the data into a database without copying and pasting the 400+ pages that it has grown to so that I can make the site database driven. My site ...

热门标签