[antlr-interest] String concatenation expression rule

franck102 franck102 at yahoo.com
Fri Nov 18 10:50:21 PST 2011


I guess I will try that if I have to, although this is an existing language (with a custom, unmaintainable parser) that is already very ambiguous, and I am concerned that parsing any sequence of pretty much anything as a string concatenation is going to make things much worse in that respect.

Franck



________________________________
 From: Jim Idle [via ANTLR] <ml-node+s1301665n7009129h70 at n2.nabble.com>
To: franck102 <franck102 at yahoo.com> 
Sent: Friday, November 18, 2011 6:47 PM
Subject: Re: String concatenation expression rule
 

Don't make the parser trap that error as a syntax error. It is a semantic 
error and so you should parser any operand type, then reject the incorrect 
types with code. 

jim 


> -----Original Message----- 
> From: [hidden email] [mailto:antlr-interest- 
> [hidden email]] On Behalf Of franck102 
> Sent: Friday, November 18, 2011 8:53 AM 
> To: [hidden email] 
> Subject: Re: [antlr-interest] String concatenation expression rule 
> 
> Hi Bart, thanks very for the quick reply. 
> 
> I should have made it clear that concatenating is only legal if at 
> least one of the operands is a string literal - that is where I am 
> having an issue. In other terms the input 
> 
> 3 3<EOF> 
> should cause a syntax error, while 
> 3 "" 3<EOF> 
> should not (and evaluates to the string "33") 
> 
> 
> Franck 
> 
> 
> 
> ________________________________ 
>  From: Bart Kiers [via ANTLR] <ml- 
> [hidden email]> 
> To: franck102 <[hidden email]> 
> Sent: Friday, November 18, 2011 1:11 PM 
> Subject: Re: String concatenation expression rule 
> 
> 
> On Fri, Nov 18, 2011 at 12:39 PM, franck102 <[hidden email]> wrote: 
> 
> 
> > I am writing a grammar for a fairly complex expression language, and 
> in 
> > particular I need to support string concatenation which is performed 
> simply 
> > by separating string literals with a space; and which automatically 
> > converts 
> > other expressions to a string if needed to concatenate: 
> > "a" "b" -> "ab" 
> > 2+3 "mm" -> "5mm" 
> > 
> > I suspect I could use predicates to write a rule like this: 
> > 
> > concatExpression 
> >        :        ( expression | STRING_LITERAL )+ { apply only if at 
> least 
> > one of the elements is a string literal }? 
> > 
> > Is there a way to achieve this? The alternative formulations I can 
> think of 
> > are pretty messy... 
> > 
> > As far as I understand it, you don't need any predicate. I see a 
> concat-expression has a lower precedence than addition, in which case 
> this 
> could do the trick: 
> 
> grammar T; 
> 
> options { 
>   output=AST; 
> } 
> 
> tokens { 
>   ROOT; 
>   CONCAT; 
> } 
> 
> parse 
>   :  (expression ';')* EOF -> ^(ROOT expression*) 
>   ; 
> 
> expression 
>   :  (add -> add) (add+ -> ^(CONCAT add+))? 
>   ; 
> 
> add 
>   :  atom (('+' | '-')^ atom)* 
>   ; 
> 
> atom 
>   :  Number 
>   |  String 
>   |  '(' expression ')' -> expression 
>   ; 
> 
> Number : '0'..'9'+ ('.' '0'..'9'+)?; 
> String : '"' ~'"'* '"'; 
> Space  : ' ' {skip();}; 
> 
> You can test it with the following class: 
> 
> import org.antlr.runtime.*; 
> import org.antlr.runtime.tree.*; 
> import org.antlr.stringtemplate.*; 
> 
> public class Main { 
>   public static void main(String[] args) throws Exception { 
>     String src = "42 - 2; 2 + 3 \"mm\"; \"a\" \"b\" 4-3-2 \"c\"; \"pi = 
> \" 
> 3.14159;"; 
>     TLexer lexer = new TLexer(new ANTLRStringStream(src)); 
>     TParser parser = new TParser(new CommonTokenStream(lexer)); 
>     CommonTree root = (CommonTree)parser.parse().getTree(); ; 
>     System.out.println(new DOTTreeGenerator().toDOT(root)); 
>   } 
> } 
> 
> Regards, 
> 
> Bart. 
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address 
> 
> 
> ________________________________ 
> 
> If you reply to this email, your message will be added to the 
> discussion below: 
> http://antlr.1301665.n2.nabble.com/String-concatenation-expression-
> rule-tp7007921p7008016.html 
> To unsubscribe from String concatenation expression rule, click here. 
> NAML 
> 
> -- 
> View this message in context: 
> http://antlr.1301665.n2.nabble.com/String-concatenation-expression-
> rule-tp7007921p7008934.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 
List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address


________________________________
 
If you reply to this email, your message will be added to the discussion below:
http://antlr.1301665.n2.nabble.com/String-concatenation-expression-rule-tp7007921p7009129.html 
To unsubscribe from String concatenation expression rule, click here.
NAML

--
View this message in context: http://antlr.1301665.n2.nabble.com/String-concatenation-expression-rule-tp7007921p7009337.html
Sent from the ANTLR mailing list archive at Nabble.com.


More information about the antlr-interest mailing list