Context Free Grammar: <program> -><stmtlist>\'end\' <stmtlist> -><stmt><stmtlist
ID: 3887496 • Letter: C
Question
Context Free Grammar: <program> -><stmtlist>'end' <stmtlist> -><stmt><stmtlist>| ----- note: this is equivalent to <stmt>|<stmt><stmtlist> <stmt> -> ID '=' <expr> |'read' ID |'write' <expr> <expr> -> <term> {('+'|'-')<term>} <term> -> <factor> {('*'|'/') <factor>} <factor> -> ID | INT_CONSTANT |'('<expr>')' The possible set of tokens, represented with regular expressions, includes: ASSIGN -> = PLUS -> + MINUS -> - TIMES -> * DIV -> / LPAREN ->( RPAREN -> ) ID -> letter(letter|digit)* Except read, write, and end. INT_CONSTANT -> digit digit* LETTER ->[a-zA-Z] DIGIT ->[0-9] Your task is to implement a scanner and a parser for this simple language.(in any programming language of your choice.)
Explanation / Answer
Program -> ClassDeclList ClassDecl -> class id { VarDeclList MethodDeclList } VarDecl -> Type id ; MethodDecl -> Type id ( FormalList ) { VarDeclList StmtList return Exp ; } Formal -> Type id Type -> int [ ] | boolean | int | id Stmt -> { StmtList } | if ( Exp ) Stmt else Stmt | while ( Exp ) Stmt | System.out.println ( Exp ) ; | id = Exp ; | id [ Exp ] = Exp ; Exp -> Exp Op Exp | Exp [ Exp ] | Exp . length | Exp . id ( ExpList ) | integer | true | false | id | this | new int [ Exp ] | new id ( ) | ! Exp | ( Exp ) Op -> && | < | + | - | * ExpList -> Exp ExpRest | ExpRest -> , Exp ExpRest | FormalList -> Type id FormalRest | FormalRest -> , Type id FormalRest | ClassDeclList = ClassDeclList VarDecl | MethodDeclList = MethodDeclList MethodDecl | VarDeclList = VarDeclList VarDecl |
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.