Skip to content

Commit

Permalink
Improvement to the text.
Browse files Browse the repository at this point in the history
Signed-off-by: Ken Domino <kenneth.domino@domemtech.com>
  • Loading branch information
kaby76 authored and parrt committed Jun 28, 2023
1 parent f7bea2a commit ec00837
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions doc/target-agnostic-grammars.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
# Writing target-agnostic grammars

Some grammars may require side-effecting
actions or [semantic predicates](https://github.com/antlr/antlr4/blob/dev/doc/predicates.md)
Some grammars may require
[semantic predicates](https://github.com/antlr/antlr4/blob/dev/doc/predicates.md)
in order to add context-sensitive parsing to what would normally be a context-free grammar.

For example:
* In Fortran90, [lines that being with a 'C' in column 1
denote a comment](https://github.com/antlr/grammars-v4/blob/43fbb16fec1d474d38a603cc6a6bcbe5edf07b1e/fortran/fortran90/slow/hw.f90#L1).
These types of comments should be tokenized and placed on a comment token stream,
not the default token stream. But, if the 'C' does not begin in
are comments](https://github.com/antlr/grammars-v4/blob/43fbb16fec1d474d38a603cc6a6bcbe5edf07b1e/fortran/fortran90/slow/hw.f90#L1),
and should be placed on a token stream other than the default token stream.
But, if the 'C' does not begin in
column 1, then the input is invalid and should be flagged as so.
```fortran
c Hello World.
c This is a syntax error because 'c' does not start in column 1
program hello
print *, 'Hello World!'
print *, 'Hello World!'
end
```
* In CSharp, double angle-brackets `'>>'` can either mean
* In CSharp, two [greater-than signs](https://util.unicode.org/UnicodeJsps/character.jsp?a=003E)
`'>>'` can either mean
[a right shift expression](https://github.com/antlr/grammars-v4/blob/43fbb16fec1d474d38a603cc6a6bcbe5edf07b1e/csharp/examples/AllInOneNoPreprocessor.cs#L657C15-L657C17)
or [a declaration that contains a type with
nested templates](https://github.com/antlr/grammars-v4/blob/master/csharp/examples/AllInOneNoPreprocessor.cs#L463C33-L463C35).
Since lexers in Antlr are not parser aware,
the lexer must tokenize the double angle-brackets as two separate tokens, one for each angle bracket.
A semantic predicate should be added to disallow a space between the angle brackets when parsing an
the lexer must tokenize the two greater-than signs as two separate tokens.
A semantic predicate should be added to disallow a space between the two greater-than signs when parsing an
expression.
```C#
class Foo {
void Func()
{
int x = 1000 > > 2; // syntax error if a space exists in the double-angle bracket
}
Dictionary<int, List<int> > mapping; // nested template declaration, valid
void Func()
{
int x = 1000 > > 2; // syntax error if a space exists in the double greater-than sign
}
Dictionary<int, List<int> > mapping; // nested template declaration, valid
}
```

Expand Down

0 comments on commit ec00837

Please sign in to comment.