// Augmented with actions to generate abstract syntax trees.

package parse;

import syntaxtree.*;

action code {:

// An abbreviation for creating identifiers.
static Identifier id(String s) {
return new Identifier(s);
}

:};

parser code {:

errormsg.ErrorMsg errorMsg;

public void syntax_error(java_cup.runtime.Symbol current) {
report_error("Syntax error (" + current.sym + ")", current);
}

public void report_error(String message, java_cup.runtime.Symbol info) {
errorMsg.error(info.left, message);
}

public Grm(java_cup.runtime.Scanner scan, errormsg.ErrorMsg err) {
this(scan);
errorMsg = err;
}

:};

// Notice the type declarations for ID and INTEGER_LITERAL; these are
// the only tokens with non-null semantic values.

terminal String ID;
terminal Integer INTEGER_LITERAL;
terminal CLASS, PUBLIC, STATIC, VOID, MAIN, STRING, RETURN,

INT, BOOLEAN, IF, ELSE, WHILE, PRINTLN, LENGTH,

TRUE, FALSE, THIS, NEW,

LBRACE, RBRACE, LPAREN, RPAREN, LBRACK, RBRACK,

SEMICOLON, COMMA, ASSIGN, AND, LT, PLUS, MINUS, TIMES,

DOT, EXCLAMATION;

// The syntaxtree classes are summarized on page 99. Note that they have been
// augmented with a pos field to record their position in the source file.

non terminal syntaxtree.Program Program;
non terminal syntaxtree.Exp Exp;
non terminal syntaxtree.Statement Statement;

non terminal syntaxtree.MainClass MainClass;

non terminal syntaxtree.ClassDecl ClassDecl;

non terminal syntaxtree.VarDecl VarDecl;

non terminal syntaxtree.MethodDecl MethodDecl;

non terminal syntaxtree.FormalList FormalRestStar;

non terminal syntaxtree.FormalList FormalList;

non terminal syntaxtree.Formal FormalRest;

non terminal syntaxtree.Type Type;

non terminal syntaxtree.ExpList ExpRestStar;

non terminal syntaxtree.ExpList ExpList;

non terminal syntaxtree.Exp ExpRest;

non terminal syntaxtree.ClassDeclList ClassDeclStar;

non terminal syntaxtree.VarDeclList VarDeclStar;

non terminal syntaxtree.MethodDeclList MethodDeclStar;

non terminal syntaxtree.StatementList StatementStar;

precedence left AND;

precedence left LT;

precedence left PLUS, MINUS;

precedence left TIMES;

precedence left EXCLAMATION;

precedence left DOT, LBRACK, RBRACK;

start with Program;

// Fill in the grammar below...
Program ::= MainClass:m1 ClassDeclStar:c1

{: RESULT = new Program(m1left, m1, c1 ); :}

;

MainClass ::= CLASS ID:i1 LBRACE:l1 PUBLIC STATIC VOID MAIN LPAREN STRING LBRACK RBRACK ID:i2 RPAREN LBRACE Statement:s1 RBRACE RBRACE

{: RESULT = new MainClass(l1left, id(i1), id(i2), s1); :}

;

 

ClassDeclStar ::=

{: RESULT = new ClassDeclList(); :}

| ClassDeclStar:cs ClassDecl:c1

{: cs.addElement(c1);RESULT=cs; :}

;

 

ClassDecl ::= CLASS ID:id1 LBRACE VarDeclStar:vs MethodDeclStar:ms RBRACE

{: RESULT = new ClassDeclSimple(id1left, id(id1), vs,ms); :}

;

 

VarDeclStar ::=

{: RESULT = new VarDeclList(); :}

| VarDeclStar:vs VarDecl:v1

{: vs.addElement(v1);RESULT=vs; :}

;

 

VarDecl ::= Type:t1 ID:id1 SEMICOLON

{: RESULT = new VarDecl(t1left, t1, id(id1)); :}

;

 

MethodDeclStar ::=

{: RESULT = new MethodDeclList(); :}

| MethodDeclStar:ms MethodDecl:m1

{: ms.addElement(m1);RESULT=ms; :}

;

 

MethodDecl ::= PUBLIC Type:t1 ID:id1 LPAREN FormalList:fl RPAREN LBRACE VarDeclStar:vs StatementStar:st RETURN Exp:e1 SEMICOLON RBRACE

{: RESULT = new MethodDecl(t1left, t1,id(id1), fl, vs, st, e1); :}

;

 

FormalList ::=

{: RESULT = new FormalList(); :}

| Type:t ID:id1 FormalRestStar:fs

{: FormalList fn = new FormalList();

fn.addElement(new Formal(tleft,t,id(id1)));

for (int i=0;i<fs.size();i++)

fn.addElement(fs.elementAt(i));RESULT=fn; :}

;

 

FormalRestStar ::=

{: RESULT = new FormalList(); :}

| FormalRestStar:fs FormalRest:f1

{: fs.addElement(f1);RESULT=fs; :}

;

 

FormalRest ::= COMMA Type:t ID:id

{:RESULT = new Formal(tleft,t,id(id));:}

;

 

Type ::= INT:i

{: RESULT = new IntegerType(ileft); :}

| BOOLEAN:b

{: RESULT = new BooleanType(bleft); :}

| INT:i LBRACK RBRACK

{: RESULT = new IntArrayType(ileft); :}

| ID:id1

{: RESULT = new IdentifierType(id1left, id1); :}

;

 

StatementStar ::=

{: RESULT = new StatementList(); :}

| Statement:s StatementStar:st

{:StatementList s1= new StatementList();

s1.addElement(s);

for(int i=0;i<st.size();i++){s1.addElement(st.elementAt(i));}

RESULT=s1; :}

;

 

 

Statement ::= LBRACE:l StatementStar:SS RBRACE

{: RESULT = new Block(lleft, SS ); :}

| IF:i LPAREN Exp:e1 RPAREN Statement:s1 ELSE Statement:s2

{: RESULT = new If(ileft, e1, s1, s2); :}

| WHILE:w LPAREN Exp:e1 RPAREN Statement:s1

{: RESULT = new While(wleft, e1, s1); :}

| PRINTLN:p LPAREN Exp:e1 RPAREN SEMICOLON

{: RESULT = new Print(pleft, e1); :}

| ID:id1 ASSIGN:a Exp:e1 SEMICOLON

{: RESULT = new Assign(aleft, id(id1), e1); :}

| ID:id1 LBRACK:l Exp:e1 RBRACK ASSIGN Exp:e2 SEMICOLON

{: RESULT = new ArrayAssign(lleft, id(id1), e1, e2); :}

;

 

Exp ::= Exp:e1 AND:a Exp:e2

{: RESULT = new And( aleft, e1, e2 ); :}

| Exp:e1 LT:lt Exp:e2

{: RESULT = new LessThan( ltleft, e1, e2 ); :}

| Exp:e1 PLUS:p Exp:e2

{: RESULT = new Plus( pleft, e1, e2 ); :}

| Exp:e1 MINUS:m Exp:e2

{: RESULT = new Minus( mleft, e1, e2 ); :}

| Exp:e1 TIMES:t Exp:e2

{: RESULT = new Times( tleft, e1, e2 ); :}

| Exp:e1 LBRACK:l Exp:e2 RBRACK:r

{: RESULT = new ArrayLookup( lleft, e1, e2 ); :}

| Exp:e DOT:d LENGTH:l

{: RESULT = new ArrayLength( eleft, e ); :}

| Exp:e DOT:d ID:i LPAREN ExpList:el RPAREN

{: RESULT = new Call( dleft, e, id( i ), el ); :}

| INTEGER_LITERAL:i

{: RESULT = new IntegerLiteral(ileft, i.intValue()); :}

| TRUE:t

{: RESULT = new True( tleft ); :}

| FALSE:f

{: RESULT = new False( fleft ); :}

| ID:i

{: RESULT = new IdentifierExp( ileft, i ); :}

| THIS:t

{: RESULT = new This( tleft ); :}

| NEW INT:i LBRACK Exp:e RBRACK

{: RESULT = new NewArray( ileft, e ); :}

| NEW ID:i LPAREN RPAREN

{: RESULT = new NewObject( ileft, id( i ) ); :}

| EXCLAMATION Exp:e

{: RESULT = new Not( eleft, e ); :}

| LPAREN Exp:e RPAREN

{: RESULT = e; :}

;

 

ExpRestStar ::=

{: RESULT = new ExpList(); :}

| ExpRestStar:ers ExpRest:er1

{: ers.addElement(er1);RESULT=ers; :}

;

 

ExpList ::=

{: RESULT = new ExpList(); :}

| Exp:e1 ExpRestStar:ers

{: ExpList el1 = new ExpList();

el1.addElement(e1);

for(int i=0; i<ers.size();i++){el1.addElement(ers.elementAt(i));}

RESULT = el1; :}

;

 

ExpRest ::= COMMA Exp:e1

{: RESULT=e1; :}

;

www.cpccci.com
www.cpcwebsolutions.com
www.cpcwebdevelopment.com