[il-antlr-interest: 27819] Re: [antlr-interest] Tree grammar bizarre behaviour?
At 13:34 8/02/2010, Iwud H8u wrote: token1 returns [ObjectA a] : ^(C_ID (conId=classId {$a=evaluateAndReturnAsObjectA($conId.text); }) ); token2 returns [ObjectA m] : ^(C_ID (id=classId {$m=evaluateAndReturnAsObjectA($id.text); }) ); [...] Oddly, the Java methods generated for the methods have the following return types : --- MyTree.token1_return token1() { ... } --- ObjectA token2() { .. } I'm slightly at my wits end, understanding whats going on. What am I missing here? Generally, a rule will return a value directly if it only has one return value, or a structure containing all the return values if it has more than one. Evidently something about the token1 rule is making it want to return multiple values; perhaps it's the context in which it's used in another rule? If you have a look at the contents of the structure it might give you an additional hint. If it does return a *_return structure, then it will have a member matching the declared value. (ie. token1_return will contain an ObjectA called a.) List: http://www.antlr.org/mailman/listinfo/antlr-interest Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address -- You received this message because you are subscribed to the Google Groups il-antlr-interest group. To post to this group, send email to il-antlr-inter...@googlegroups.com. To unsubscribe from this group, send email to il-antlr-interest+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/il-antlr-interest?hl=en.
[il-antlr-interest: 27822] Re: [antlr-interest] Parse 1 - N repeats
Hi Adam, You could handle it in (plain) programming logic inside your grammar. Here's a little demo: grammar Test; @parser::members { public static void main(String[] args) throws Exception { String text = FIELD1\n+ REPEATING_GROUP fields=2 min=0, max=20\n+ FIELD2\n+ FIELD3\n+ FIELD4; ANTLRStringStream in = new ANTLRStringStream(text); TestLexer lexer = new TestLexer(in); CommonTokenStream tokens = new CommonTokenStream(lexer); new TestParser(tokens).parse(); } class Repeat { final ListString fieldList; final int fields; final int min; final int max; Repeat(int fields, int min, int max) { this.fieldList = new ArrayListString(fields); this.fields = fields; this.min = min; this.max = max; } boolean done() { return fieldList.size() == fields; } public String toString() { return String.format(fields=\%s, min=\%d, max=\%d, fieldList, min, max); } } } parse : ( rp=repeat {System.out.println(repeat :: +$rp.r);} | id=Identifier {System.out.println(field :: +$id.text);} )* EOF ; repeat returns [Repeat r] : Identifier '' 'fields' '=' fields=Identifier '' '' 'min' '=' min=Identifier ',' 'max' '=' max=Identifier '' {$r = new Repeat(Integer.valueOf($fields.text), Integer.valueOf($min.text), Integer.valueOf($max.text));} (id=Identifier {$r.fieldList.add($id.text); if($r.done()) return $r;} )* ; Identifier : ('a'..'z' | 'A'..'Z' | '0'..'9' | '_' )+ ; WhiteSpace : ( ' ' | '\t' | '\r' | '\n' ) {skip();} ; As you see, whenever the size of the fieldList hits the total, $r is being returned (and no more id=Identifier will be eaten). When you compile and execute the TestParser class, the following is being printed: field :: FIELD1 repeat :: fields=[FIELD2, FIELD3], min=0, max=20 field :: FIELD4 Regards, Bart. On Mon, Feb 8, 2010 at 1:56 PM, Adam Connelly adam.rpconne...@googlemail.com wrote: Hi, Sorry if this is answered elsewhere, but I'm not really sure what to search for. I'm trying to parse a language that includes repeating groups. The problem is that they don't include terminators, so you can't tell the difference between the last item in the group, and the next section. Here's an example: FIELD1 REPEATING_GROUP fields=2 min=0, max=20 FIELD2 FIELD3 FIELD4 ... fields specifies the number of fields contained in the group. At the moment I've got the following rules, but the problem is that it means that the repeating group rule doesn't get its fields associated with it: recordDefinition :RECORD (IDENTIFIER | repeatingGroup)+ ; repeatingGroup :IDENTIFIER '' NUMBER_OF_FIELDS '=' fieldCount=NUMBER '' '' NUMBER_OF_REPEATS '=' min=NUMBER ',' max=NUMBER '' ; Ideally I could do something like: repeatingGroup :IDENTIFIER '' NUMBER_OF_FIELDS '=' fieldCount=NUMBER '' '' NUMBER_OF_REPEATS '=' min=NUMBER ',' max=NUMBER '' IDENTIFIER{1, $fieldCount} ; But I know you can't do that. What would the best way be to go about parsing this? Can I build an AST then modify it to put the identifiers for the repeating group in the right place. Cheers, Adam List: http://www.antlr.org/mailman/listinfo/antlr-interest Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address List: http://www.antlr.org/mailman/listinfo/antlr-interest Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address -- You received this message because you are subscribed to the Google Groups il-antlr-interest group. To post to this group, send email to il-antlr-inter...@googlegroups.com. To unsubscribe from this group, send email to il-antlr-interest+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/il-antlr-interest?hl=en.
[il-antlr-interest: 27826] Re: [antlr-interest] Creating AST with third party grammar
Please, repply on the simple question: how access to generated ast? The documantation for 3.0 a thery uggly and don't contains necessary information. I know how to do it in the version 2.0 (there are method getAST() in Parser class), but this method removed in version 3.0! -- View this message in context: http://n2.nabble.com/Creating-AST-with-third-party-grammar-tp4524691p4537748.html Sent from the ANTLR mailing list archive at Nabble.com. List: http://www.antlr.org/mailman/listinfo/antlr-interest Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address -- You received this message because you are subscribed to the Google Groups il-antlr-interest group. To post to this group, send email to il-antlr-inter...@googlegroups.com. To unsubscribe from this group, send email to il-antlr-interest+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/il-antlr-interest?hl=en.
[il-antlr-interest: 27827] Re: [antlr-interest] Creating AST with third party grammar
Greetings! On Mon, 2010-02-08 at 14:58 -0800, tahiti wrote: Please, repply on the simple question: how access to generated ast? The documantation for 3.0 a thery uggly and don't contains necessary information. I know how to do it in the version 2.0 (there are method getAST() in Parser class), but this method removed in version 3.0! If you look closely at the source code generated by the Tool for your parser --- and it is one of the strengths of ANTLR that you can actually read and debug the generated code --- you will see that each Parser in your grammar has a corresponding method in the generated source code. And that method has a return type of a structure one of whose fields is `tree`, the AST generated by that rule. Hope this helps... -jbb List: http://www.antlr.org/mailman/listinfo/antlr-interest Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address -- You received this message because you are subscribed to the Google Groups il-antlr-interest group. To post to this group, send email to il-antlr-inter...@googlegroups.com. To unsubscribe from this group, send email to il-antlr-interest+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/il-antlr-interest?hl=en.
[il-antlr-interest: 27829] Re: [antlr-interest] How to specify target language from the command line?
Before my Visual Studio custom tool calls the ANTLR tool, it adds language=XXX just after options\s*{. Not the nicest way, but it gets the job done. Now I can use my language independent lexer, parser and tree grammars in ANTLRWorks for debugging and use the same file in the target environment. Some options really concern the recognizer, such as tokenVocab, output, ASTLabelType, filter, etc. and I can't see a reason why you would like to change those independent of the contents of the file. Some options however, like language or superclass, could be independent of the target. Would it be an idea to be able to specify extra options from the command line? Ciao, Ranco From: Ranco Marcus Sent: maandag 1 februari 2010 10:47 To: antlr-interest@antlr.org Subject: How to specify target language from the command line? When no language is specified in the options section, ANTLR defaults to Java. Is it possible to specify the default language when calling org.antlr.tool? Many of my grammars (lexer, parser, and tree) have no language specific code and specifying the language (CSharp2 in my case) unnecessarily constrains the use of these grammars. To generate recognizers, I have written a (basic) custom tool for Visual Studio that calls ANTLR when changes are made to a .g file. I would like to specify the target language in the command line arguments. This way, the grammar could remain language independent and there would be no need to remove the language option each time the grammar is being debugged in ANTLRWorks. Btw, Terence, congratulations on the new Language Implementation Patterns book. I read (most of) it last week and really enjoyed it! Best regards, Ranco Marcus List: http://www.antlr.org/mailman/listinfo/antlr-interest Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address -- You received this message because you are subscribed to the Google Groups il-antlr-interest group. To post to this group, send email to il-antlr-inter...@googlegroups.com. To unsubscribe from this group, send email to il-antlr-interest+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/il-antlr-interest?hl=en.