Tiger language parser for compiler

759 views Asked by At

I am now trying to build a compiler using the book Modern Compiler Implementation in C (the Tiger book). I am now stuck in the parser phase. I have built my grammar and tried to run it but still get some syntax error. I tried my lexer and it appears to work.

This is my code for yacc/bison:

    %{
#include <stdio.h>
#include "util.h"
#include "errormsg.h"
#include <stdio.h>

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

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


%union {
    int pos;
    int ival;
    string sval;
    }

%token <sval> ID STRING
%token <ival> INT

%token 
  COMMA COLON SEMICOLON LPAREN RPAREN LBRACK RBRACK 
  LBRACE RBRACE DOT 
  PLUS MINUS TIMES DIVIDE EQ NEQ LT LE GT GE
  AND OR ASSIGN
  ARRAY IF THEN ELSE WHILE FOR TO DO LET IN END OF 
  BREAK NIL
  FUNCTION VAR TYPE 

%start program

%nonassoc DO OF
%nonassoc THEN /* ELSE must come after THEN! */
%nonassoc ELSE
%left SEMICOLON
%left ASSIGN
%left OR
%left AND
%nonassoc EQ NEQ GT LT GE LE
%left PLUS MINUS
%left TIMES DIVIDE
%left UMINUS

%%

program:    exp;

dec:    tyDec;
dec:    varDec;
dec:    funDec;

tyDec:  TYPE ID EQ ty;

ty:     ID; 
ty:     arrTy;
ty:     recTy;

arrTy:  ARRAY OF ID;

recTy:  LBRACE fieldDecs RBRACE;

fieldDecs:  /* empty */ ;
fieldDecs:  fieldDec;
fieldDecs:  fieldDec COMMA fieldDecs;

fieldDec:   ID COLON ID;

funDec: FUNCTION ID LPAREN fieldDecs RPAREN EQ exp;
funDec: FUNCTION ID LPAREN fieldDecs RPAREN COLON ID EQ exp;

varDec: VAR ID ASSIGN exp;
varDec: VAR ID COLON ID ASSIGN exp;

lValue: ID;
lValue: subscript;
lValue: fieldExp;

subscript:  lValue LBRACK exp RBRACK;

fieldExp:   lValue DOT ID;

exp:    lValue;
exp:    NIL;
exp:    INT;
exp:    STRING;
exp:    seqExp;
exp:    negation;
exp:    callExp;
exp:    infixExp;
exp:    arrCreate;
exp:    recCreate;
exp:    assignment;
exp:    ifThenElse;
exp:    ifThen;
exp:    whileExp;
exp:    forExp;
exp:    BREAK;
exp:    letExp;

seqExp: LPAREN exps RPAREN;

exps:   /* empty */ ;
exps:   exp;
exps:   exp SEMICOLON exps;

negation:   MINUS exp %prec UMINUS ;

callExp:    ID LPAREN expsc RPAREN ;

expsc:  /* empty */ ;
expsc:  exp;
expsc:  exp COMMA expsc;

infixExp:   arithmExp;
infixExp:   boolExp;
infixExp:   compExp;

arithmExp:  exp PLUS exp;
arithmExp:  exp MINUS exp;
arithmExp:  exp TIMES exp;
arithmExp:  exp DIVIDE exp;

boolExp:    exp AND exp;
boolExp:    exp OR exp;

compExp:    exp EQ exp;
compExp:    exp NEQ exp;
compExp:    exp GT exp;
compExp:    exp LT exp;
compExp:    exp GE exp;
compExp:    exp LE exp;

arrCreate:  ID LBRACK exp RBRACK OF exp;

recCreate:  ID LBRACE fieldCreates RBRACE;

fieldCreates:   /* empty */ ;
fieldCreates:   fieldCreate;
fieldCreates:   fieldCreate COMMA fieldCreates;

fieldCreate:    ID EQ exp;

assignment: lValue ASSIGN exp;

ifThenElse: IF exp THEN exp ELSE exp;

ifThen: IF exp THEN exp;

whileExp:   WHILE exp DO exp;

forExp: FOR ID ASSIGN exp TO exp DO exp;

letExp: LET decs IN exps END;

decs:   dec;
decs:   dec decs;

/* ERROR RECOVERY */

exp:    LPAREN error RPAREN;

seqExp: error SEMICOLON exp;

When I run my parser on a sample tiger code, I get some syntax error, including at the beginning.

Could you please help me to sort it out? Thanks!

0

There are 0 answers