Link to home
Start Free TrialLog in
Avatar of epitsi
epitsi

asked on

Simple grammar in javacc

It's been 4 years since i have taken a compiler class. I am now trying to create a parser, and I am using javacc for creating the grammar. I will try to demonstrate my problem in the following simple scenario: Let's suppose I need a grammar to read any number of numbers and/or letters, but also to be able and recognize "(" set of characters ")". I have tried something like:

Expression() :
{
<WORD>
| <NUM>
| "(" Foo() ")"
}

Foo() :
{
"0"
| "A"
}

The problem here is that it refuses to recognize exactly what I need since I get a warning that 0 and A will be treated as NUM and WORD respectively. What is the best way to tackle this? Also can someone please give me an example of a recursive rule? Is that even possible? By recursive I refer to the rule calling itself, something like:

Foo() :
{
Foo() ( Foo() )*
}
Avatar of humanonomics
humanonomics
Flag of India image

you mean to say that you want to implement a Grammar mechanism in Java ?
Can you please elaborate more on your needs ?
Avatar of epitsi
epitsi

ASKER

I will try to keep it as simple as it gets then. I need a javacc file which will be able to parse any text and output it, unless it sees "(0)" in which case it will output "found". For example:

Input: a012(0)
Output: a012found
What I meant was you want a Java program to do the parsing ?
ASKER CERTIFIED SOLUTION
Avatar of epitsi
epitsi

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