Question: For this exercise you will write a parser using a parser generator . You will describe the Cool grammar in an appropriate input format and

For this exercise you will write a parser using a parser generator. You will describe the Cool grammar in an appropriate input format and the parser generator will generate actual code (in OCaml). You will also write additional code to unserialize the tokens produced by the lexer stage and to serialize the abstract syntax tree produced by your parser.

For this exercise you will write a parser using a parser generator.

using the cool syntax figure above translate the following javascript syntax into OCaml and name the OCaml file parser.mly

You will describe the Cool grammar in an appropriate input format and

the parser generator will generate actual code (in OCaml). You will also

example of some of the translation is shown below here.

write additional code to unserialize the tokens produced by the lexer stage

Cool Syntax program class feature [class;]+ class TYPE inherits TYPE] { [feature;]* } ID(( formal [, formal]']): TYPE { expr} ID : TYPE ( expr ; ]' esac new TYPE isvoid expr expr + expr expr - expr expr * expr expr / expr expr expr CASE ELSE ESAC FI IF IN LET LOOP OF POOL THEN WHILE token AT COLON COMMA DOT LARROW LBRACE LPAREN RARRON RBRACE RPAREN RPAREN SEMI stoken EQUALS DIVIDE LE LT MINUS NOT PLUS TILDE TIMES Stoken TRUE FALSE stoken IDENTIFIER INTEGER STRING TYPE Stoken EOF estart cool program rule /* the entry point */ Stype cool program_rule cool_program_rule : class_list EOF { $1 } ; class_list : /* nothing */ { []} I cool_class SEMI class_list { $1 :: $3} cool_class : CLASS TYPE LBRACE feature_list RBRACE { ClassNoInherits ($2, $4)) : feature_list : /* nothing */ { [] } | feature SEMI feature_list i si :: $3 } : feature : IDENTIFIER COLON TYPE ( AttributeNoInit($1, $3) } Cool Syntax program class feature [class;]+ class TYPE inherits TYPE] { [feature;]* } ID(( formal [, formal]']): TYPE { expr} ID : TYPE ( expr ; ]' esac new TYPE isvoid expr expr + expr expr - expr expr * expr expr / expr expr expr CASE ELSE ESAC FI IF IN LET LOOP OF POOL THEN WHILE token AT COLON COMMA DOT LARROW LBRACE LPAREN RARRON RBRACE RPAREN RPAREN SEMI stoken EQUALS DIVIDE LE LT MINUS NOT PLUS TILDE TIMES Stoken TRUE FALSE stoken IDENTIFIER INTEGER STRING TYPE Stoken EOF estart cool program rule /* the entry point */ Stype cool program_rule cool_program_rule : class_list EOF { $1 } ; class_list : /* nothing */ { []} I cool_class SEMI class_list { $1 :: $3} cool_class : CLASS TYPE LBRACE feature_list RBRACE { ClassNoInherits ($2, $4)) : feature_list : /* nothing */ { [] } | feature SEMI feature_list i si :: $3 } : feature : IDENTIFIER COLON TYPE ( AttributeNoInit($1, $3) }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!