Professional Documents
Culture Documents
CA448: Compiler Construction 1, 2009/2010. I hereby certify that the work presented and
the material contained herein is my own except where explicitly stated references to other material
are made.
Eoin Costelloe
When the lexer reaches the end of file character (EOF), it checks for any errors found.
Parser:
However this produces left recursive problems from DispatchExpr, InstanceofExpr and
BinaryExpr. I first converted the DispatchExpr to the following:
void DispatchExpr() : {}
{
<ID> <LEFT_BRACKET> Expr() ExtraExprs() <RIGHT_BRACKET>
| Expr() <MEMBER_REFERENCE> <ID> <LEFT_BRACKET> Expr() ExtraExprs()
<RIGHT_BRACKET>
}
void Expr() : {}
{
AssignExpr() ExprPrime()
| DispatchExpr() ExprPrime()
| NewExpr() ExprPrime()
| CastExpr() ExprPrime()
| UnaryExpr() ExprPrime()
| ConstExpr() ExprPrime()
| VarExpr() ExprPrime()
| <LEFT_BRACKET> Expr() <RIGHT_BRACKET> ExprPrime()
}
I then put all left recursive rules into the ExprPrime and converted them as follows:
void ExprPrime() : {}
{
DispatchExpr() ExprPrime()
| InstanceofExpr() ExprPrime()
| BinaryExpr() ExprPrime()
| {}
}
I also removed all the starting Expr from each rule in BinaryArithExpr, BinaryCompExpr
and BinaryLogicExpr as follows:
void BinaryArithExpr() : {}
{
<PLUS_SIGN> Expr()
| <MINUS_SIGN> Expr()
| <MULTIPLY_SIGN> Expr()
| <DIVIDE_SIGN> Expr()
| <MODULO_SIGN> Expr()
}
void BinaryCompExpr() : {}
{
<EQUALS_COMPARATOR> Expr()
| <NOT_EQUALS_COMPARATOR> Expr()
| <LESS_THAN_COMPARATOR> Expr()
| <LESS_THAN_OR_EQUAL_COMPARATOR> Expr()
| <GREATER_THAN_COMPARATOR> Expr()
| <GREATER_THAN_OR_EQUAL_COMPARATOR> Expr()
}
void BinaryLogicExpr() : {}
{
<AND_BOOLEAN_SIGN> Expr()
| <OR_BOOLEAN_SIGN> Expr()
}
void VarExpr() : {}
{
<ID>
}
I have added a lookahead of 2 in Stmt to choose between ExprStmt and DeclStmt and avoid
the conflict with <ID>. I have joined DispatchExpr VarExpr and AssignExpr to avoid a common
prefix of <ID> as follows:
void DispatchVarOrAssignExpr() : {}
{
<ID> DispatchOrAssignExprPrime()
}
void DispatchVarOrAssignExprPrime() : {}
{
DispatchExpr()
| AssignExpr()
| {} //basic var expression
}
I have added a lookahead of 2 to avoid the conflict with <ID> “)”. I have also joined
CastExpr and BracketExpr to avoid a common prefix of <LEFT_BRACKET> as follows:
void CastOrBracketExpr() : {}
{
<LEFT_BRACKET> CastOrBracketExprPrime()
}
void CastOrBracketExprPrime() : {}
{
CastExpr()
| Expr() <RIGHT_BRACKET> //bracket expression
}
I then added in code to return an Abstract Syntax Tree (AST) which is documented in
bantamjava/api/html/index.html. Please the default parent node for Class is “Object” and the default
reference expression for DispatchExpr is “this”.