2

Does antl4 support adaptive grammars that allows the user to specify new rules, such as enforcing the number of arguments specified in a function declaration?

Example: Base language includes the following token definitions:

  • Token#1 is defined as [a-z][0-9]*
  • token#2 is [A-Z][0-9]*

The uppercase are reserved for function names, and the lower case are reserved for variables passed to the function.

The user can "declare" Fxy, and every following instance of F has to have two variables. I want the parser to enforce the "new rule".

Perhaps this is standard fair in compilers, I know the compilers I use for C, python, etc. bitch when I don't pass the right number of arguments for a function I declared elsewhere. However, I don't know how to do this myself in my own grammar; the undergrad course I took on compilers was more than 15 years ago and I don't recall it including how to enforce # of arguments required for user declared functions. I've written some simple languages with five keywords and scoping (brackets), somewhat akin to the calculator examples you find in textbooks, but nothing complex.

So, I guess what I also want to know is whether the ANTLR books will teach me how to do this (don't want to spend the money if the books don't explain what I want to achieve).

Linus Fernandes
  • 498
  • 5
  • 30
Lugh
  • 107
  • 9
  • There are syntax errors and semantic errors. The parser can check only first ones. Do not try to make the parser "too intelligent", the grammar would be hard to maintain then. – ibre5041 May 19 '15 at 06:25
  • Do I understand you correct: You have a base language having a rule for functions with arbitrary length parameter list, e.g. 'function : Token2 Token1*'. Now you want to allow another to override this definition to allow exactly 2 arguments,e.g. 'function : Token2 Token1 Token1'. – CoronA May 19 '15 at 12:19
  • @CoronA Grosenberg got me and answered my question, thank-you. – Lugh May 20 '15 at 01:17

2 Answers2

1

An adaptive grammar would be a grammar for producing another grammar. But that is not what you are really asking for or how parsers are typically used for the purposes you describe.

In general, a grammar defines the allowed syntax of the language (or DSL) while the visitors to the tree generated from the grammar determine if the language semantics are met. Whether a call to a named function contains the right number and type of parameters is a question of semantics, not syntax.

Consider the following grammar snippet:

decl   : fName AS FUNC LPAREN params? RPAREN body ;
func   : FUNC fName LPAREN params? RPAREN body ;
params : param ( COMMA param)* ;
param  : type pname ;

stmnt  : fname LPAREN ( pname ( COMMA pname )* )? RPAREN SEMI ;

It allows standard functions (methods) and it allows new functions to be declared. The stmnt rule allows a named function to be called.

Whether the type and number of pnames is correct is a question of semantics that can only be answered in an analysis implemented by walking the generated tree: is there a function with the given fname, do the number of pnames and params match, do the types match or are they convertable, etc.

The Antlr books will help. You may wish to spend some time looking at the repository grammars to get a better feel for how different languages can be described by a grammar.

GRosenberg
  • 5,843
  • 2
  • 19
  • 23
  • Thank-you. I know how to address the issue by walking the tree... I believe i misunderstood the wikipedia page on adaptive grammars as to what they represented, and got the impression that I could empower users to dynamically update the syntax directly, and got mildly excited by that. – Lugh May 20 '15 at 01:27
  • Thank-you for clarifying. Would you be able to offer any other reasons why I should switch to ANTLR from lexx/yacc? – Lugh May 20 '15 at 02:43
  • Antlr generally allows a better separation between the grammar and user code? Antlr takes a slightly different approach to handling the analysis phase/tree walking [as discussed in the last few paragraphs of this answer](http://stackoverflow.com/questions/30312976/antlr-4-parser-grammar/30335862#30335862). – GRosenberg May 20 '15 at 05:02
0

An adaptive grammar is essentially a grammar for a "self-extensible" parser that "learns" new grammar rules from its input. ANTLR does not appear to support adaptive grammars, but there are some other parser generators that do support them, such as dypgen, which is based on the GLR parsing algorithm.

Anderson Green
  • 30,230
  • 67
  • 195
  • 328