[antlr-interest] Need help with simple grammar

Jim Idle jimi at temporal-wave.com
Wed Apr 11 09:44:43 PDT 2007


Steve,

 

In the beta 6 release I was out of sync with the codegen template
interface. Please download the latest snapshot and you will find that it
works as advertised.

 

Jim

 

From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Steve Karam
Sent: Wednesday, April 11, 2007 7:01 AM
To: Johannes Luber
Cc: antlr-interest at antlr.org
Subject: Re: [antlr-interest] Need help with simple grammar

 

Thanks Johannes, this compiled great as Java...any idea how I can
compile it as C?

I tried adding this:

options {
    language=C;
}

But I received these errors:

ANTLR Parser Generator  Version 3.0b6 (Jan 31, 2007)  1989-2007
error(10):  internal error: group C does not satisfy interface
ANTLRCore: missing templates [lexerRulePropertyRef_stop]

error(10):  internal error: group C does not satisfy interface
ANTLRCore: mismatched arguments on these templates [outputFile(LEXER,
PARSER, TREE_PARSER, actionScope, actions, docComment, recognizer, name,
tokens, tokenNames, rules, cyclicDFAs, bitsets, buildTemplate, buildAST,
rewrite, profile, backtracking, synpreds, memoize, numRules, fileName,
ANTLRVersion, generatedTimestamp, trace, scopes, superClass, literals),
optional headerFile(LEXER, PARSER, TREE_PARSER, actionScope, actions,
docComment, recognizer, name, tokens, tokenNames, rules, cyclicDFAs,
bitsets, buildTemplate, buildAST, rewrite, profile, backtracking,
synpreds, memoize, numRules, fileName, ANTLRVersion, generatedTimestamp,
trace, scopes, superClass, literals), listLabel(label, elem)]

error(10):  internal error: mql.g : java.util.NoSuchElementException: no
such attribute: buildAST in template context [headerFile]
org.antlr.stringtemplate.StringTemplate.rawSetAttribute(StringTemplate.j
ava:739)
org.antlr.stringtemplate.StringTemplate.setAttribute(StringTemplate.java
:600)
org.antlr.codegen.CodeGenerator.genRecognizer(CodeGenerator.java:341)
org.antlr.Tool.processGrammar(Tool.java:329)
org.antlr.Tool.process(Tool.java:266)
org.antlr.Tool.main(Tool.java:69)

I know that this is probably because I'm missing class declarations in
the language file...but I thought grammar files were always
cross-platform?  Maybe I'm expecting too much.  ;)

Johannes Luber wrote: 

Steve Karam wrote:
  

	I've never worked with any sort of parsing engine before; I
think I am
	getting this, but still getting caught up with the huge amount
of
	syntax.  Could anyone help me with a basic grammar file that can
handle
	these basic types of commands?
	 
	get data from file
	put data in file
	change data in file
	get metadata about file
	change metadata about file
	get dependencies of file
	get dependents of file
	get statistics of file
	 
	Basically it boils down to:  command what preposition target.
There
	will of course be more than this in the end, but I really need
this
	basic understanding first before I can even think about what
else I'll
	need to tackle.
	 
	What would a grammar file for this look like?  I'd prefer using
C++, but
	I don't mind Java if that's all you know!
	 
	    

 
A basic ANTLR3 grammar without semantic checks (i.e. invalid sentences
still allowed) would be:
 
grammar CommandLanguage;
 
tokens {
        GET='get';
        PUT='put';
        CHANGE='change';
        DATA='data';
        METADATA='metadata';
        DEPENDENCIES='dependencies';
        DEPENDENTS='dependents';
        STATISTICS='statistics';
        FROM='from';
        IN='in';
        ABOUT='about';
        OF='of';
        FILE='file';
}
 
command
        :       sentence*;
 
sentence
        :       verb object preposition target
        ;
 
verb
        :       GET
        |       PUT
        |       CHANGE
        ;
 
object
        :       DATA
        |       METADATA
        |       DEPENDENCIES
        |       DEPENDENTS
        |       STATISTICS
        ;
 
preposition
        :       FROM
        |       IN
        |       ABOUT
        |       OF
        ;
 
target
        :       FILE
        ;
 
You still have to change the target rule to allow arbitrary filenames
and include predicates which filter the invalid sentences out (supposing
all your examples were entirely inclusive). I suggest to buy the Beta
Book (http://www.pragmaticprogrammer.com/titles/tpantlr/index.html) for
more in-depth information.
 
Best regards,
Johannes Luber
 
  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070411/d6d39cf9/attachment.html 


More information about the antlr-interest mailing list