In: Computer Science
Javacc Parser Generation
A) Select a non-java programming language
B)Select a set of rules (for at least three programming concepts e.g., conditionals, classes, loops etc.) from the language and write down the grammar rules (in BNF or EBNF)
C) Create a .jj template file for this grammar (include 1) and 2) as comments in the file). Compile the .jj file and run the corresponding .java file
D) As comments in the same jj file, show some expressions that were tested for the parser you have created using javacc.
// recognize arithmetic expressions
PARSER_BEGIN(Calc0) // must
define parser class
public class Calc0 {
public static void main
(String args []) {
Calc0 parser = new
Calc0(System.in);
for (;;)
try {
if (parser.expr() ==
-1)
System.exit(0);
} catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
}
PARSER_END(Calc0)
SKIP: //
defines input to be ignored
{ " " | "\r" | "\t"
}
TOKEN: //
defines token names
{ < EOL: "\n" >
| < CONSTANT: (
<DIGIT> )+ > // re: 1 or more
| < #DIGIT: ["0" - "9"]
> // private re
}
int expr(): //
expr: sum \n
{} // -1
at eof, 0 at eol
{ sum() <EOL> {
return 1; }
| <EOL> { return 0;
}
| <EOF> { return -1;
}
}
void sum(): //
sum: product { +- product }
{}
{ product() ( ( "+" | "-" )
product() )*
}
void product():
// product: term { *%/ term }
{}
{ term() ( ( "*" | "%" |
"/" ) term() )*
}
void term(): //
term: +term | -term | (sum) | number
{}
{ "+" term()
| "-" term()
| "(" sum() ")"
| <CONSTANT>
}
{}
Note: Plzzz don' t give dislike.....Plzzz comment if u have any problem i will try to resolve it.......