Welcome to Scribd. Sign in or start your free trial to enjoy unlimited e-books, audiobooks & documents.Find out more
Standard view
Full view
of .
Look up keyword
Like this
0 of .
Results for:
No results containing your search query
P. 1
Compiler Construction

Compiler Construction

Ratings: (0)|Views: 981|Likes:
Published by eoincos
Description of Lexical and Syntax Analysis for Bantam Java. Based on DCU module CA448: Compiler Construction
Description of Lexical and Syntax Analysis for Bantam Java. Based on DCU module CA448: Compiler Construction

More info:

Published by: eoincos on Jan 27, 2010
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less





A report submitted to Dublin City University, School of Computing for module
CA448: Compiler Construction 1
, 2009/2010. I hereby certify that the work presented andthe material contained herein is my own except where explicitly stated references to other materialare made.
Lexical and Syntax Analyser
Eoin CostelloeCA448: Compiler Construction 1
The lexer allows the following:skips which first exclude the areas between tokens such as (tabs, spaces, new lines) that we donot think are important for the lexer. We also skip single line comments (//) and multi linecomments (/* */). These multi line comments do not allow nested comments for simplicitysake.simple tokens such as punctuation, keywords, signs and comparators. boolean specific tokens such as boolean constants (true, false) and boolean signs (!, &&, ||).strings. A valid string starts with a quote (“) represented in the lexer as “\””, any characterswithin the string except the new line characters (\n), the return character (\r) and ends with aquote. A string cannot go beyond its maximum length (5000 characters excluding the 2quotes).integers and identifiers. Integer constants are allowed to have leading zeros and have no sign(assumed to be positive). We do not allow for integer constants which exceed the variablesize (greater than 2^31). Identifiers must begin with a letter but can be followed by anyamount of numbers letters or “_” characters.error checking for unsupported special symbols in strings, unfinished strings, unsupportedcharacters that are not within a comment or a string constant and unfinished comments.Supported special symbols are \n (newline), \t (tab), \” (double quote), \\ (backslash) and \f (form feed). All other special symbols are unsupported. An unfinished string is one thatcontains a new line character or a return character. An unfinished comment is one without anending */.When the lexer reaches the end of file character (EOF), it checks for any errors found.
The parser is a copy of the bantam java grammar rules:Program -> (Class)+Class -> class identifier [ extends identifier ] { (Member)* }Member -> Method | FieldMethod -> identifier identifier ( Formal (, Formal)* ) {Stmt RetnStmt }Field -> identifier identif ier [ = Expr ] ;Formal -> identifier identifier Stmt -> ExprStmt | DeclStmt | IfStmt | WhileStmt | BlockStmtRetnStmt -> return [ Expr ] ;ExprStmt -> Expr ;DeclStmt -> identifier identifier = Expr ;IfStmt -> if ( Expr ) Stmt [ else Stmt ]WhileStmt -> while ( Expr ) StmtBlockStmt -> { Stmt }Expr -> AssignExpr | DispatchExpr | NewExpr | InstanceofExpr | CastExpr | BinaryExpr |UnaryExpr | ConstExpr | VarExpr | ( Expr )AssignExpr -> VarExpr = Expr DispatchExpr -> [ Expr . ] identifier ( Expr (, Expr)* ) NewExpr -> new identifier ( )InstanceofExpr -> Expr instanceof identifier CastExpr -> ( identifier ) ( Expr )BinaryExpr -> BinaryArithExpr | BinaryCompExpr | BinaryLogicExpr 
UnaryExpr -> UnaryNegExpr | UnaryNotExpr ConstExpr -> int const | boolean const | string constBinaryArithExpr -> Expr + Expr | Expr − Expr | Expr * Expr | Expr / Expr | Expr % Expr BinaryCompExpr -> Expr == Expr | Expr ! = Expr | Expr < Expr | Expr <= Expr | Expr > Expr |Expr >= Expr BinaryLogicExpr -> Expr && Expr | Expr || Expr UnaryNegExpr -> − Expr UnaryNotExpr -> ! Expr VarExpr -> [ identifier . ] identifier However this produces left recursive problems from DispatchExpr, InstanceofExpr andBinaryExpr. 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>}I then added an ExprPrime to the end of each Expr as follows: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()

Activity (5)

You've already reviewed this. Edit your review.
1 hundred reads
Aakshan liked this
sewhenu liked this
Prabhu liked this
abablo liked this

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->