Click here to Skip to main content
15,672,517 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
looking for a tutorial and ideally source code, for a simple interpreter that reads a grammar file containing Token definitions [using regular expressions]and the allowed syntax for the Tokens. I.e. BNF or similar with a set of rules. The interpreter will read input from a source file and interpret this based on the specified grammar. The solution must be able to react dynamically to changing the BNF. For example, if the BNF is modified and the interpreter run again then a different behaviour may be expected as a result of modifying the grammar.

for example:-

file: grammar.txt contains:-

split = "\t " // tab or hard space to sp0lit tokens
INT = "(0-9)+" // integer: single digit followed by any number of digits
ADD = "add" // literal
MINUS = "minus" // literal

Expr ::= Term (ADD Term | MINUS Term)*
Term ::= (INT | Expr )

probably got the grammar wrong but you get the idea ?!

Looking for solution in JAVA please

What I have tried:

just search google

found many a project on compilers, not many interpreters. the compiler projects create source code from the BNF. That's not what I want

the interpreters are hard coded so cannot respond to changes in the BNF. That's not what I want either

found this but it requires CUP Jlex ?? whatrever they are, which muddies the water and the BNF is not something I recognise or want to work wit; it also creates code which isn't what I want - that's more like a compiler.

BNF Converter Tutorial[^]
thanks in advance
Updated 5-Jul-17 2:11am

1 solution

You could try using ANTLR[^].
Share this answer
ninjaef 5-Jul-17 9:47am    
Mmmm. me thinks you obviously didnt read read the question !

ANTLR creates a parser from grammar, the parser needs to be compiled each time the grammar changes.

CPallini 5-Jul-17 11:48am    
I read it, of course. You are going to build a meta-compiler or, possibly, a meta-interpreter. ANTLR is still a useful tool for the purpose. Of course you have to provide to ANTLR a fixed grammar to ANTLR, but that doesnt matter, because it would be the grammar of your compiler, not the grammar provided as input to your compiler.
ninjaef 6-Jul-17 2:15am    
No, that does not answer the question

I already have coded a Tokenizer and a LL1 Parser. This converts the BNF notation in a text file to a parse tree (a grammar tree to be precise). I am now stuck.
Normally, one would use hardcoded functions that model the syntax of the language, to read the input program text [file] and parse it according to the language definition.
This is different.
I want to be able to change the BNF and the compiler(interpreter) is able to correctly compile(interpret) a new program text without requiring me to change the code of the compiler(interpreter)
So ANTLR isn't the answer
CPallini 6-Jul-17 2:41am    
OK, so, good luck.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900