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() )*
}
epitsiAsked:
Who is Participating?
 
epitsiConnect With a Mentor Author Commented:
If it would be more straightforward to write a Java program than using javacc then that would also work.
0
 
humanonomicsCommented:
you mean to say that you want to implement a Grammar mechanism in Java ?
Can you please elaborate more on your needs ?
0
 
epitsiAuthor Commented:
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
0
 
humanonomicsCommented:
What I meant was you want a Java program to do the parsing ?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.