Parsing string interpolation in ANTLR -


I am working on a simple string manipulation DSL for internal purposes, and I want to support the language string string I would like to use in Ruby.

For example:

  name = "bob" msg = "hello $ {name}!" Print (msg) # Print "Hello Bob!"  

I am trying to implement my parser in ANTLRv3, but with the use of ANLR I am very inexperienced so I am unsure of implementing this facility. So far, I have specified my string of text in laser, but in this case I have to clearly handle the interpolation material in the parser.

The literal grammar of my current string looks like this:

  STRINGLITERAL: '' '(StringEscapeSeq | ~ (' \\ '|' '' | '\ r '|' | \ N ');' 'String StringEscapeSeq:' \\ '(' t '|' '' '' '' '' '' '' | '\\' | '$' | ('0' .. '9'));  

To handle the string in the pars literally, everything else should be done as it should, the web search did not give any information. How to get started on this?

I am not an ANLR expert But here is a possible grammar:

  grammar str; parse: (space) * statement (space) * ';') + (space) * eof; statement: print; assignment; Print: 'print' ('(identifier | string literal)'); assignment: identifier (space) * '=' (space) * string literals; string literal: '' '(identifier | esquequane | normalcair | space | Interpolation) * ''; Interpolation: '$ {' identifier '}'; Identifier: ('a' .. 'z' | 'a' .. 'z' | '_') ('a' .. 'Z' | 'A' .. 'z' | '_' | '0' .. '9') *; Escape Sequences: '\\' Specialthes; Specifically: '' '\' '|' $ ''; Space: ('' | '' \ t '|' \ r '|' \ n '); General words: ~ Specialty;  

As you are seeing, there are some (space) * -a inside the example grammar. The reason for this is that stringLiteral is a parser rule for lexer-rule For this, when the source file is detected, Lexzer can not know whether a white space is part of the string, or there is a place inside the source file that can be ignored.

I tested with a little Java class for example and did all the expectations:

  / * same grammar, but now with a little Java in it Code * / grammar Str; @parser :: header {package antlrdemo; Import java.util.hashMap;} @lexer :: header {package antlrdemo;} @parser :: members {HashMap & LT; String, string & gt; Wars = new Hashmap & lt; String, string & gt; ();} Pars: (space) * statement (space) * ';') + (space) * EOF; Statement: Print | Assignments; Print: 'Print' ('(id = identifier {system.out.println ("& gt;" + vars.get ($ id. Text));} | st = stringLiteral {System.out.println ("& gt; ; "+ $ St.value);}) ')'; Assignment: id = identifier (space) * '=' (space) * st = stringLiteral {vars.put ($ id.text, $ st.value);}; StringLiteral Returns [String Value]: '' '{StringBuilder B = New StringBuilder ();} (ID = Identifier {b.append ($ id.text);} | es = EscapeSequence {b.append ($ es.text) ;} | C = (normal cesar | space) {b.pend ($ cctet);} | in = interpolation {b.append (wars.jet ($ IN.text.substring (2, $ IN.text.length ( ) -1)));}) * '' '{$ value = b.toString ();}; Interpolation: '$ {' i = identifier '}'; Identifier: ('a' .. 'z' | 'a' .. 'z' | '_') ('a' .. 'z' | 'a' .. 'z' | '_' | '0 '..' 9 ') *; Escape Sequences: '\\' Specialthes; Specifically: '' '\\' | '$'; Space: ('' | '' \ t '|' \ r '|' \ n '); General words: ~ Specialty;  

and a class with a main method to test all this.

  package antlrdemo; Import org.antlr.runtime *; Public class aNTLRDemo {public static void accusation throws acceleration {String source = "name = \" bob \ "; \ N" + "msg = \" hello $ {name} \ "; \ N" + "Print (msg); \ n" + "print (\" $ bye \\ yet \ ") \"); "; ANTLRStringStream = In the new ANTLRStringStream (source); StrLexer lexer = new StrLexer (in); CommonTokenStream token = new CommonTokenStream (lexer); StrParser parser = new StrParser (token); parser.parse ();}}  

which produces the following output:

  & gt; Hello Bob & gt; By $$!  

Again, I'm not an expert, but this (atleast) gives you a method to solve it.

H.H.

< / Div>

Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

php - jQuery AJAX Post not working -