Skip to content

Commit f38ffe5

Browse files
committed
Ensure that empty lexer alternatives are handled properly
Fixes #50.
1 parent f85b80c commit f38ffe5

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

grammarinator/process.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,12 @@ def build_expr(node, parent_id):
428428
graph.add_edge(frm=parent_id, to=lambda_id)
429429
return
430430

431-
for child in node.element() if isinstance(node, ANTLRv4Parser.AlternativeContext) else node.lexerElements().lexerElement():
431+
children = node.element() if isinstance(node, ANTLRv4Parser.AlternativeContext) else node.lexerElements().lexerElement()
432+
if isinstance(node, ANTLRv4Parser.LexerAltContext) and not children:
433+
graph.add_edge(frm=parent_id, to=lambda_id)
434+
return
435+
436+
for child in children:
432437
build_expr(child, parent_id)
433438

434439
elif isinstance(node, (ANTLRv4Parser.ElementContext, ANTLRv4Parser.LexerElementContext)):
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) 2022 Renata Hodovan, Akos Kiss.
3+
*
4+
* Licensed under the BSD 3-Clause License
5+
* <LICENSE.rst or https://opensource.org/licenses/BSD-3-Clause>.
6+
* This file may not be copied, modified, or distributed except
7+
* according to those terms.
8+
*/
9+
10+
/*
11+
* This test checks whether the empty lexer and parser alternatives
12+
* are handled properly.
13+
*/
14+
15+
// TEST-PROCESS: {grammar}.g4 -o {tmpdir}
16+
// TEST-GENERATE: {grammar}Generator.{grammar}Generator -r start -o {tmpdir}/{grammar}%d.txt
17+
// TEST-ANTLR: {grammar}.g4 -o {tmpdir}
18+
// TEST-PARSE: -p {grammar}Parser -l {grammar}Lexer -r start {tmpdir}/{grammar}%d.txt
19+
20+
grammar EmptyAlternatives;
21+
22+
start: | EMPTY ;
23+
24+
EMPTY : 'a' ( | 'b') ;

0 commit comments

Comments
 (0)