Writing an ANTLR action "in between" multiplicity - antlr

I'm working on an ANTLR grammar that looks like...
A : B+;
...and I'd like to be able to perform an action before and after each instance of B. For example, I'd like something like...
A : A {out("Before");} B {out("After");}
| {out("Before");} B {out("After");};
So that on the input stream A B B I would see the output...
Before
After
Before
After
Of course the second example isn't valid ANTLR syntax because of the left recursive rule. Is there a way to accomplish what I want with proper ANTLR syntax?
I should also mention that there are other ways of reaching the B rule so simply surrounding the B rule with before and after won't work.

Doesn't something like
A : ({out("Before");} B {out("After");})+;
work?

Related

Why is this grammar not LL(1)?

I am doing some homework, and was asked to show that the grammar A -> aAa | ε is not LL(1). From everything that I have seen, the answer I have so far is that since the First and the Follow sets of A contain a. Is this correct or am I thinking about something the wrong way?

Improve syntactic predicate error messages?

I am trying to improve the error messages antlr gives and noticed that syntactic predicates seem to be the root of the bad error messages.
This is the one I am currently working on. Here is an example of the grammar's structure. Sorry that I cannot provide the actual grammar. Hopefully this illustrates the point though.
defs
: (a) => a | b
;
a
: A B c
;
//b is actually much further down the chain and due to ordering can't be moved up.
b
: A c
;
The issue is that for example if you have the tokens "A B D". The error message you get is from the 'b' rule. I want the error message to be from the 'a' rule. Meaning if "A B" is matched then I want an error if 'c' isn't matched.
I thought maybe you could do this
a
: (A B) => A B c | {EmitErrorMessage("error");}
;
You should relax the syntactic predicate in defs instead of adding one to a.
defs
: (A B) => a
| b
;
This will cause the parser to choose the first alternative and enter the a rule based on just the two symbols A B.

bnf grammar checking

is there a tool to check my bnf grammar?
For example:
<assign>::=<var>=<expr>
<var>::=A|B|C
<expr>::=<expr>+<expr>
|<var>
A = B + C is a valid statement according to my bnf grammar and
A = B * C is not.
Is there a tool to check if given statement is valid or not?
Have used this while in my CS classes, I think it can pretty much do what you're looking for, that is, validating a statement with a given grammar.

antlr add syntactic predicate

For the following rule :
switchBlockLabels
: ^(SWITCH_BLOCK_LABEL_LIST switchCaseLabel* switchDefaultLabel? switchCaseLabel*)
;
I got an error:"rule switchBlockLabels has non-LL descision due to recursive rule invocations reachable from alts 1,2".And I tried to add syntactic predicate to solve this problem.I read the book "The Definitive ANTLR Reference".And Now I am confused that since there is no alternatives in rule switchBlockLabels,then no decision need to be made on which one to choose.
Is anyone can help me?
Whenever the tree parser stumbles upon, say, 2 switchCaseLabels (and no switchDefaultLabel in the middle), it does not know to which these switchCaseLabels belong. There are 3 possibilities the parser can choose from:
2 switchCaseLabels are matched by the 1st switchCaseLabel*;
2 switchCaseLabels are matched by the 2nd switchCaseLabel*;
1 switchCaseLabel is matched by the 1st switchCaseLabel*, and one by the 2nd switchCaseLabel*.
and since the parser does not like to choose for you, it emits an error.
You need to do something like this instead:
switchBlockLabels
: ^(SWITCH_BLOCK_LABEL_LIST switchCaseLabel* (switchDefaultLabel switchCaseLabel*)?)
;
That way, when there are only switchCaseLabels, and no switchDefaultLabel, these switchCaseLabels would be always matched by the first switchCaseLabel*: there is no ambiguity anymore.

Ambiguous grammar?

hi
there is this question in the book that said
Given this grammer
A --> AA | (A) | epsilon
a- what it generates\
b- show that is ambiguous
now the answers that i think of is
a- adjecent paranthesis
b- it generates diffrent parse tree so its abmbiguous and i did a draw showing two scenarios .
is this right or there is a better answer ?
a is almost correct.
Grammar really generates (), ()(), ()()(), … sequences.
But due to second rule it can generate (()), ()((())), etc.
b is not correct.
This grammar is ambiguous due ot immediate left recursion: A → AA.
How to avoid left recursion: one, two.
a) Nearly right...
This grammar generates exactly the set of strings composed of balanced parenthesis. To see why is that so, let's try to make a quick demonstration.
First: Everything that goes out of your grammar is a balanced parenthesis string. Why?, simple induction:
Epsilon is a balanced (empty) parenthesis string.
if A is a balanced parenthesis string, the (A) is also balanced.
if A1 and A2 are balanced, so is A1A2 (I'm using too different identifiers just to make explicit the fact that A -> AA doesn't necessary produces the same for each A).
Second: Every set of balanced string is produced by your grammar. Let's do it by induction on the size of the string.
If the string is zero-sized, it must be Epsilon.
If not, then being N the size of the string and M the length of the shortest prefix that is balanced (note that the rest of the string is also balanced):
If M = N then you can produce that string with (A).
If M < N the you can produce it with A -> AA, the first M characters with the first A and last N - M with the last A.
In either case, you have to produce a string shorter than N characters, so by induction you can do that. QED.
For example: (()())(())
We can generate this string using exactly the idea of the demonstration.
A -> AA -> (A)A -> (AA)A -> ((A)(A))A -> (()())A -> (()())(A) -> (()())((A)) -> (()())(())
b) Of course left and right recursion is enough to say it's ambiguous, but to see why specially this grammar is ambiguous, follow the same idea for the demonstration:
It is ambiguous because you don't need to take the shortest balanced prefix. You could take the longest balanced (or in general any balanced prefix) that is not the size of the string and the demonstration (and generation) would follow the same process.
Ex: (())()()
You can chose A -> AA and generate with the first A the (()) substring, or the (())() substring.
Yes you are right.
That is what ambigious grammar means.
the problem with mbigious grammars is that if you are writing a compiler, and you want to identify each token in certain line of code (or something like that), then ambigiouity wil inerrupt you in identifying as you will have "two explainations" to that line of code.
It sounds like your approach for part B is correct, showing two independent derivations for the same string in the languages defined by the grammar.
However, I think your answer to part A needs a little work. Clearly you can use the second clause recursively to obtain strings like (((((epsilon))))), but there are other types of derivations possible using the first clause and second clause together.