Can someone help me with how to resolve this ambiguity?

42 views
Skip to first unread message

Peng Xiao

unread,
Dec 9, 2022, 10:23:11 PM12/9/22
to antlr-discussion
The question is also pasted here:

I need to parse something like below:


Client->iFrame.Initialise() { 
  res = Server.loadStaticResource() 
  if (res.status == 404 || res.status == 503) {   <1> 
    throw Error()
  } 
== Initialisation done! ==                                      <2> 
Client->iFrame.load(data) { 
  moreStatements() 
}

To add more context, this is to be used to render a sequence diagram like below:

question-antlr.png

It is a Java-like syntax, except that we support a Divider (shown as == Initialisation done! == in the example). Note that:

  1. both <1> and <2> uses ==.
  2. It is treated as a Divider only if it appears at the beginning of the line (after spaces removed).
  3. Any character can be used between the starting == and ending == except for changelines.
  4. There could be more = in the Divider component, such as === 3 equals ===.

How should I implement that?

What I have tried?

I was looking for a way to use lookahead in the Lexer, but could not find any good examples. I am currently limiting the content between starting == and ending == to ONE word only.

My implementation is here:

  1. The parser: https://github.com/ZenUml/core/blob/b84c9ca0a6d023c5e520bf83a225e4931db134fc/src/g4/sequenceParser.g4#L88-L89
  2. The lexer: https://github.com/ZenUml/core/blob/main/src/g4/sequenceLexer.g4

Eric Vergnaud

unread,
Dec 10, 2022, 5:29:15 AM12/10/22
to antlr-di...@googlegroups.com
If it’s treated as a divider only when at beginning of line, then one way would be to have a DIVIDER token including the LF such as the following: ‘\n==[^\n]*\n’
Since it’s longer then ‘\n' it will win during recognition. 

Le 10 déc. 2022 à 04:23, Peng Xiao <eagle...@gmail.com> a écrit :

The question is also pasted here:

I need to parse something like below:


Client->iFrame.Initialise() { 
  res = Server.loadStaticResource() 
  if (res.status == 404 || res.status == 503) {   <1> 
    throw Error()
  } 
== Initialisation done! ==                                      <2> 
Client->iFrame.load(data) { 
  moreStatements() 
}

To add more context, this is to be used to render a sequence diagram like below:

<question-antlr.png>

It is a Java-like syntax, except that we support a Divider (shown as == Initialisation done! == in the example). Note that:

  1. both <1> and <2> uses ==.
  2. It is treated as a Divider only if it appears at the beginning of the line (after spaces removed).
  3. Any character can be used between the starting == and ending == except for changelines.
  4. There could be more = in the Divider component, such as === 3 equals ===.

How should I implement that?

What I have tried?

I was looking for a way to use lookahead in the Lexer, but could not find any good examples. I am currently limiting the content between starting == and ending == to ONE word only.

My implementation is here:

  1. The parser: https://github.com/ZenUml/core/blob/b84c9ca0a6d023c5e520bf83a225e4931db134fc/src/g4/sequenceParser.g4#L88-L89
  2. The lexer: https://github.com/ZenUml/core/blob/main/src/g4/sequenceLexer.g4

--
You received this message because you are subscribed to the Google Groups "antlr-discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to antlr-discussi...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/antlr-discussion/cb088c04-7aca-4ce9-9774-d929299ef779n%40googlegroups.com.
<question-antlr.png>

Reply all
Reply to author
Forward
0 new messages