[antlr-interest] Implementing main method in ANTLR

William v Doorn williamvdoorn at gmail.com
Wed Apr 14 11:56:51 PDT 2010


Let's take this as an example:

nummer x() { return 3+5; }

void main_method() { print x(); }

Here, in a normal language - the nummer x will only be executed when the
main method calls it. Now in my grammar, it just walks over the lines in the
files and executes the statements inside the function. This is not what I
want. I only want to execute the pieces of code which are called in the main
method.

I've attached my grammar/tree walker,

Thanks;

William van Doorn

2010/4/14 Cliff Hudson <cliff.s.hudson at gmail.com>

> I guess I don't understand what exactly it is you are asking.  What do you
> mean a 'main method per file'?  What other functions in which files?
>
>
> On Wed, Apr 14, 2010 at 11:46 AM, William v Doorn <williamvdoorn at gmail.com
> > wrote:
>
>> Hello --
>>
>> I don't fully understand it yet, now my top-level rule is just a rule who
>> defines a list of lines. But - if I start parsing at the main method - those
>> other functions in the files aren't parsed and when I'm parsing them and I
>> have a print statement there it will print right away.
>>
>> So I kind of need to parse everything and store it in my java-classes,
>> without executing anything in the functions - is this possible, would this
>> require much editting?
>>
>> Thanks,
>>
>> William van Doorn
>>
>>  2010/4/14 Cliff Hudson <cliff.s.hudson at gmail.com>
>>
>> A simple main method would be one which determines where the input is
>>> going to be coming from, then creates an input stream based on that location
>>> (for instance, an ANTLRStringStream for something from a string, or
>>> ANTLRFileStream for something from a file.)  Then you create your Lexer and
>>> pass that input to the lexer.  You then create a token stream, such as
>>> CommonTokenStream and pass it your lexer.  You then create your parser and
>>> pass it your token stream.  You then invoke which ever rule you wish on your
>>> parser - typically your top level rule whatever that may be.  If you are
>>> using a tree parser, your parser call will return a tree.  You would then
>>> instantiate your tree parser, and invoke one of its rules passing in your
>>> tree.  Repeat this sequence for as many inputs as you desire.
>>>
>>> On Wed, Apr 14, 2010 at 11:27 AM, William v Doorn <
>>> williamvdoorn at gmail.com> wrote:
>>>
>>>> Hello,
>>>>
>>>> I've been working with ANTLR for quite some time now - and now I want to
>>>> implement a main method per file. However, I really have no idea how to
>>>> do
>>>> this. I'm generating AST trees and then walk over those. But, how would
>>>> I
>>>> implement a main method?
>>>>
>>>> If anyone has an idea/approach on how to go about it - please tell me,
>>>> thanks.
>>>>
>>>> William v. Doorn
>>>>
>>>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>>>> Unsubscribe:
>>>> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>>>>
>>>
>>>
>>
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Grammar.g
Type: application/octet-stream
Size: 2113 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20100414/e90d32c5/attachment.obj 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: GrammarWalker.g
Type: application/octet-stream
Size: 2209 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20100414/e90d32c5/attachment-0001.obj 


More information about the antlr-interest mailing list