[antlr-interest] Which approach for an Interpreter: Tree Grammar vs AST-Visitor

Hiran Chaudhuri Hiran.Chaudhuri at web.de
Wed Feb 2 23:00:26 PST 2011


>So would it be better to have an example like the Pie language use a
>tree grammar or is the hand-written visitor code a better approach? What
>are the pros and cons? Any help appreciated.

A pure interpreter would read statements one by one and execute them directly.
A compiler would read the whole source in one chunk and translate it before execution.
You can take any approach inbetween as well.

That said, I believe parsing the whole file and translating into an AST is not really interpretation.
But there is still the approach to parse statement for statement, and here I would definitely separate the grammar from execution code through an AST.

file: statement+ EOF;
statement: .....

My interpreter would open the file, construct parser and lexers, manage variables and scope, repeatedly ask the parser to return the AST for the next statement and send it for execution.
This way you would get the typical behaviour that syntax errors or else are detected during execution time. And if you were to change your mind towards a compiler, the grammar and AST construction would not change as you simply call the file method on the parser.

Of course it is more intersting if your interpreted language allows methods/procedures/functions as then parsing the source file would not be linear.

Hiran
___________________________________________________________
Neu: WEB.DE De-Mail - Einfach wie E-Mail, sicher wie ein Brief!  
Jetzt De-Mail-Adresse reservieren: https://produkte.web.de/go/demail02


More information about the antlr-interest mailing list