[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 474
  • Last Modified:

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() )*
}
0
epitsi
Asked:
epitsi
  • 2
  • 2
1 Solution
 
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
 
epitsiAuthor Commented:
If it would be more straightforward to write a Java program than using javacc then that would also work.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now