Link to home
Start Free TrialLog in
Avatar of msalam65
msalam65

asked on

JavaCC Grammar

Hi,

I am totally new to JavaCC and I have been looking on the web to find some good document on learning JavaCC. Till now no luck. Can somebody help me in use javacc to write a parser for the following grammar?

Start         ::= 'begin' StatementList 'end'

What exactly does writing a grammar means? Please advise.

Thanks,
Mohammed
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Avatar of msalam65
msalam65

ASKER

Thanks for the link objects. But how do I write the following grammer?

Start         ::= 'begin' StatementList 'end'

Is it a java code like this.

class Start() {

begin..

I am confused on what actually I need to write. Please help.

Thanks,
Mohammed
This is now the new home for Javacc: https://javacc.dev.java.net/
From there you can download the utility as well as read their documents (which are pretty good).
see:
https://javacc.dev.java.net/doc/javaccgrm.html
Also the zip file contains examples which are easy to follow.

>> What exactly does writing a grammar means?
It means that you define a language that describe both set of keywords(syntax) and their relation.
I find myself using JavaCC anytime I need to deal with complex syntax(input) which can not be parsed
by using String/StreamTokenizer or RegExp.

If you give more details about the syntax you need I can help writing down the (javacc) .jj file.
Basically I need you to define your syntax (using BNF is a great way for doing it) and the action[s]/return type that you need (assuming
you don't want only to validate the input but also want to get some info from it).
>> Start         ::= 'begin' StatementList 'end'
I get that 'begin' and 'end' are keywords, but what is StatementList?
Also, is there something you need returned from the evaluation or is just valid/invalid syntax?

>>Is it a java code like this.
>> class Start() {
Its a mix of both JavaCode and JavaCC grammer definitions.
Thanks aozarov. Following is the thing that I am working on which I need help in.

1.      Use javacc to write a parser for the following grammar:

Start         ::= 'begin' StatementList 'end'
StatementList ::= Statement { Statement }
Statement     ::= 'id' '=' Expr ';'
Expr          ::= Term { ( '+' | '-' ) Term }
Term          ::= Factor { ( '*' | '/' ) Factor }
Factor        ::= 'id' | 'integer' | '(' Expr ')'

Any help is greatly appreciated.

Thanks,
Mohammed
Just to verify.
1. is it homework?
2. should the parser return true/false for validating the input?
1) Yes this is an assignment for my class that I need help on.
2) The thing that I am having problem is in understanding how to write a parser. My professor hasn't explained much about this assignment and this is the first time I am trying to write a parser in javacc.

Thanks,
Mohammed
> 1. is it homework?

same rules actually apply whether its homework or not, talk to the page editor if you need more details.
ASKER CERTIFIED SOLUTION
Avatar of aozarov
aozarov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks aozarov. This helps a lot. Thanks again for your quick help. I really appreciate it.

Thanks,
Mohammed
NP. :-)