[antlr-interest] Re: Please help with my first project

cintyram <cintyram at yahoo.com> cintyram at yahoo.com
Thu Jan 23 12:24:39 PST 2003


hi ,
 there has been a long drawn discussion abt seperating grammar and
actions recently ;
It is best for you to just identify the syntax rules in one phase and
then check about their meaning [semantics ] in a later phase ;

regarding your language , i would suggest that you make open and close
follow a common syntax like other methods ..

> result = (image open k1) close k2;
result = (image.open(k1)).close(k2) ;

if you wanted to make them like operators, it is usually easier for
the user to figure out an operator if it is different from an id ;

I have never written a new language before, but since your code looks
like java, you might as well start with a java grammar, and clip off
parts which are not relevant; 

in any case, it is always a good idea to first write down in a natural
language [ english ] what you want the language to do, and liberally
provide both good and bad uses of the language ;

then you can figure out a pattern from that , and that will help you
write the grammar ;

also in the gammar, you might want to clearly delineate the duties of
each module , so lexer does just lexing, parser just checks syntax [
and also constructs a tree or fills other auxilliary structures ]

writing the lexer should be easy as you can almost copy from any
example ; 

for the parser, you might want to write only the rules with simple
statemetns for actions , so that initially you want to do only syntax
checking .. once you are sure that your grammar rules are fine, and
they check the syntax properly,
you can decide on how to handle the semantics and 
 you can start iteratively adding more actions ;


i havent written a whole lot of grammars , but i would advise you to
be prepared to change the parser rules frequently ; especially if you
are not clear on how you want to handle the semantics ,
so it is best to design backwards, setting the responsibilities of
each module for each case, and then implementing forwards ;

ie, theoretically it might be possible to write one big rule to match
all your input file ,
but you cannot use that information to do any thing easily :) 
so you want to write rules which give you pieces of information
grouped into easy chunks that are useful for a specific semantic action ;

and unless it is clearly defined before hand, what the actions expect
[ not necessarily what they do ] it is not easy to reduce the number
of iterations to the parser development phase;



well looks like im just ranting on abt common things .. but i felt it
might be useful as a reminder ;

cheers
ram



--- In antlr-interest at yahoogroups.com, "Philippe Lavoie
<philippe.lavoie at c...>" <philippe.lavoie at c...> wrote:
> Hi,
> 
> I'm running into several problems and if some kind sould wouldn't mind
> helping out, I'd really appreciate it.
> 
> I have basically this problem:
>   - how to declare context dependant grammar
> 
> I want to create a language to be used as an interface to my
> mathematical mophology library (i.e. image processing operators). I
> think that antlr is up to the challenge but unfortunately it seems I'm
> not. I've read a lot of tutorials but it seems it's not enough :(
> 
> Here is an example of a program I'd like to parse
> 
> // Start
> Image image;
> Kernel k1;
> Kernel k2;
> 
> k1 = disk(10);
> k2 = matrix(3,3);
> k2[0] = "001";
> k2[1] = "010";
> k2[2] = "100";
> 
> image.load("someimage.png");
> 
> Image result;
> result = (image open k1) close k2;
> 
> result.save("result.png");
> // End
> 
> 
> 
> I can't figure out how to restrict .load and .save to an image that
> has been declared nor can I figure out how to differentiate between a
> kernel and an image assignment since both k1 or result are viewed as
> ID by the lexer.
> 
> I could do the above in C++, but I kind of would like to have an
> interpreted syntax version like above to speed up my tests.
> 
> Thanks for any tips.
> 
> Phil


 

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



More information about the antlr-interest mailing list