You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This PR adds support for semantic predicates to Lrama, allowing conditional enabling of grammar rules based on runtime conditions.
Syntax:
```yacc
rule : {expression}? TOKEN { action }
| TOKEN { action }
;
```
The predicate {expression}? is evaluated at parse time. If it returns true (non-zero), the alternative is enabled.
Example:
```yacc
widget
: {new_syntax}? WIDGET ID NEW_ARG
{ printf("New syntax\n"); }
| {!new_syntax}? WIDGET ID OLD_ARG
{ printf("Old syntax\n"); }
;
```
Motivation / Background
Semantic predicates enable context-sensitive parsing, which is useful for:
- Version-dependent syntax (e.g., supporting both old and new language features)
- Context-sensitive keywords (e.g., async in JavaScript that behaves differently based on context)
- Conditional grammar rules based on parser state
This feature is similar to ANTLR4's semantic predicates and fills a gap in Lrama's capabilities for handling context-dependent grammars. Leading predicates (at the start of a rule) affect prediction, while trailing predicates act as validation.
Copy file name to clipboardExpand all lines: NEWS.md
+27Lines changed: 27 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,33 @@
2
2
3
3
## Lrama 0.7.1 (2025-xx-xx)
4
4
5
+
### Semantic Predicates
6
+
7
+
Support semantic predicates to conditionally enable grammar rules based on runtime conditions.
8
+
Predicates are evaluated at parse time, similar to ANTLR4's semantic predicates.
9
+
10
+
```yacc
11
+
rule : {expression}? TOKEN { action }
12
+
| TOKEN { action }
13
+
;
14
+
```
15
+
16
+
The predicate `{expression}?` is evaluated at parse time. If it returns true (non-zero), the alternative is enabled.
17
+
18
+
Example:
19
+
20
+
```yacc
21
+
widget
22
+
: {new_syntax}? WIDGET ID NEW_ARG
23
+
{ printf("New syntax\n"); }
24
+
| {!new_syntax}? WIDGET ID OLD_ARG
25
+
{ printf("Old syntax\n"); }
26
+
;
27
+
```
28
+
29
+
Predicates are compiled into static functions in the generated parser.
30
+
Leading predicates (at the start of a rule) affect prediction, while trailing predicates act as validation.
31
+
5
32
### Syntax Diagrams
6
33
7
34
Lrama provides an API for generating HTML syntax diagrams. These visual diagrams are highly useful as grammar development tools and can also serve as a form of automatic self-documentation.
0 commit comments