Question: Please help with this Compiler Design problem in C: code for parser.y: %{ #include #include util.h #include errormsg.h int yylex(void); /* function prototype */ void

Please help with this Compiler Design problem in C:

code for parser.y:

%{ #include #include "util.h" #include "errormsg.h"

int yylex(void); /* function prototype */

void yyerror(const char *s) { EM_error(EM_tokPos, "%s", s); } %}

%union { int ival; string sval; }

%error-verbose

%token ID STRING %token INT

%token COMMA COLON SEMICOLON LPAREN RPAREN LBRACK RBRACK LBRACE RBRACE PLUS MINUS TIMES DIVIDE EQ NEQ LT LE GT GE AND OR ASSIGN ARRAY IF THEN ELSE WHILE FOR DO INTTY STRINGTY FUN RETURNS RETURN %left OR %left AND %nonassoc EQ NEQ LT GT LE GE %left PLUS MINUS %left TIMES DIVIDE

%start program

%%

program: stmts

stmts: | stmt stmts

stmt: lvalue ASSIGN exp SEMICOLON // assignment | ty ID SEMICOLON // variable declaration | ty ID ASSIGN exp SEMICOLON // variable definition | ty LBRACK INT RBRACK ID ASSIGN exp SEMICOLON // array declaration | IF LPAREN exp RPAREN stmt ELSE stmt // if-then-else | WHILE LPAREN exp RPAREN stmt // while loop | FOR LPAREN ID ASSIGN exp SEMICOLON exp SEMICOLON stmt RPAREN stmt // for loop | LBRACE stmts RBRACE // code block | RETURN exp SEMICOLON // return statement |

/* Write two things that are statements in this language:

/*

exp: lvalue // variable or array access | INT // integer literal | STRING // string literal | exp PLUS exp | exp MINUS exp | exp TIMES exp | exp DIVIDE exp | exp EQ exp | exp NEQ exp | exp LT exp | exp GT exp | exp LE exp | exp GE exp | exp AND exp | exp OR exp |

/* Write two things that are expressions in this language:

*/

ty: INTTY | STRINGTY | ty LBRACK RBRACK

lvalue: ID | lvalue LBRACK exp RBRACK

args: | exp | exp COMMA args

Problems:

Please help with this Compiler Design problem in C: code for parser.y:

1. (4 points) Find the nonterminal that defines expressions in the language. In the space provided, write two sequences of characters that would be parsed as expressions. Then, find the nonterminal that defines statements in the language, and in the space provided write two inputs that would be parsed as statements. 2. (4 points) Add support for function calls to the parser. A function call is an expression, and can look like: fo sqrt(x) power(a + b, c * d) and so on, where the function name is an identifier, and the arguments (if any) are expressions. You can use the nonterminal args, which defines comma-separated lists of arguments, to match the arguments to the function call. 3. (7 points) Add support for function declarations to the parser. A function declaration is a statement, and can look like: fun fun_name returns int (int argi, s tring arg2) { // some body statements } Note that fun and returns are keywords, while fun_name and the arg s are identifiers. You can use the nonterminal ty for types. You will probably want to define a new nonterminal for the comma-separated list of function parameters (types and argument names), similar to but not the same as the args nonterminal. 1. (4 points) Find the nonterminal that defines expressions in the language. In the space provided, write two sequences of characters that would be parsed as expressions. Then, find the nonterminal that defines statements in the language, and in the space provided write two inputs that would be parsed as statements. 2. (4 points) Add support for function calls to the parser. A function call is an expression, and can look like: fo sqrt(x) power(a + b, c * d) and so on, where the function name is an identifier, and the arguments (if any) are expressions. You can use the nonterminal args, which defines comma-separated lists of arguments, to match the arguments to the function call. 3. (7 points) Add support for function declarations to the parser. A function declaration is a statement, and can look like: fun fun_name returns int (int argi, s tring arg2) { // some body statements } Note that fun and returns are keywords, while fun_name and the arg s are identifiers. You can use the nonterminal ty for types. You will probably want to define a new nonterminal for the comma-separated list of function parameters (types and argument names), similar to but not the same as the args nonterminal

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!