[antlr-interest] Help with Antlr

Sharon Li hushlee83 at yahoo.com.sg
Wed Jan 29 15:13:13 PST 2003


Thanks for advicing Ric. Basically, I need to recognize variables defined in the first few statements (i.e. #define  VARIABLE_NAME  VALUE). Next all the values assignmed after the word Homomorphism :
for example :  I(age)-->;(2)F(MAX_INT_LEN*func(1,age/DURATION)))
should be substituted into the equation as well. So the above statement should be have "age" substituted by the value in I( ?? ) and the MAX_INT_LEN and DURATION should be substituted by the value defined at the beginning of the file. 

I'm not too sure if I would require knowledge of the structure of the file to recognize the above mentioned. However the program should be flexible enough to recognize the "expr --> equation" statements for future requirements. The final equation with all the necessary substitution is what we will be looking for. Pls advice. Thanks.

Cheers,
Sharon
 Ric Klaren <klaren at cs.utwente.nl> wrote:Hi,

On Wed, Jan 29, 2003 at 08:35:30AM +0800, Sharon Li wrote:
> Hi I seem to be running into problems with Antlr, so could some kind souls
> out there pls help. 
> 
> My program needs to read a file that looks something like that :
> 
> /*--------Begining of file ----------------------------------*/
> 
> #define DURATION 100
> #define MAX_INT_LEN 10
> #define MAX_P_LEN 2
> Lsystem: 0 
> /* number of steps in the simulation */
> derivation length: 100
> /* starting string or axiom */
> /* I is an internode with parameter age,
> * L is a leaf, 
> * B is a blossom,
> * P is a petal with parameter age
> */
> Axiom: I(1)[L]/(180)[L]I(1)I(1)I(1)B
> I(age)-->I(age+1)
> P(age)-->P(age+1)
> R(ang)-->R(ang+3)
> Decomposition
> B --> R(1);(3)@o(1.5);(1)[P(1)]+(72)[P(1)]+(72)[P(1)]+(72)[P(1)]+(72)[P(1)]
> Homomorphism
> I(age)-->;(2)F(MAX_INT_LEN*func(1,age/DURATION))
> L --> +(60);(4)F(1){.g(1)[-(90)g(1).][g(2).][+(90)g(1).]}
> P(age) --> {.g(1)[-(90)g(1).][g(MAX_P_LEN*func(2,age/DURATION)).][+(90)g(1).]} 
> R(ang)--> +(ang)
> /* Label for end of L-system */
> endlsystem
> 
> /*----------- ENd of file -------------------------------------*/
> 
> "#define DURATION 100" declares a variable identifier DURATION and assigns a literal 100 to it. This value will be stored and substituted into:
> 
> P(age) --> {.g(1)[-(90)g(1).][g(MAX_P_LEN*func(2,age/DURATION)).][+(90)g(1).]} 
> 
> The program should return me the substituted string. 

So you basically want your program to return the file with all the
#define's replaced? If so then you might be better off using the C
preprocessor (usually called cpp on unix systems, binaries for windows
should be available from lotsof places look for cygwin or mingw binaries)

If you want (have?) to do this with antlr you have two options. Ask
yourself do you need to know the structure of the whole thing. If not you
can write a filtering lexer that only replaces the keywords. E.g. you make
a rule that recognizes the #define x y lines stores keys and values in a
hashtable (think there was some java container for it) and have one rule
that recognizes identifiers and checks in the hashtable for known values
and replaces them in the output when encountered. You would have to modify
the example here: http://www.antlr.org/doc/lexer.html#ANTLR_Masquerading_as_SED
a bit for that.

If you do need to know the structure your current approach is better. But
what structure do you exactly need? Do you need a parse tree to transform
lateron? Do you only need some of the expressions with replacements?

Atm I don't have much time to look at bigger things, and I didn't quite
follow the previous threads. But a few tips in debugging things:

Use the -traceLexer, -traceParser options to antlr to turn on tracing in
the generated lexer/parsers so you can see where things go wrong. Easiest
is maybe to start with checking if your lexer does what it has to do. E.g.
make a testfile that just repeatedly calls nextToken and see if all tokens
come out as expected (see for instance the examples/java/filterWithRule
example for an example main.java) After you know the lexer works ok, then
you start looking at the parser with -traceParser turned on.

HTH,

Ric
-- 
-----+++++*****************************************************+++++++++-------
---- Ric Klaren ----- klaren at cs.utwente.nl ----- +31 53 4893722 ----
-----+++++*****************************************************+++++++++-------
Wo das Chaos auf die Ordnung trifft, gewinnt meist das Chaos, weil es
besser organisiert ist. --- Friedrich Nietzsche




Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 


 Yahoo! Travel
- Valentine surprise deals. Book now!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20030130/4b888d6a/attachment.html


More information about the antlr-interest mailing list