-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/antlr/grammars-v4
- Loading branch information
Showing
266 changed files
with
6,064 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -301,5 +301,7 @@ EOF | |
cat xx.m | ||
echo ======== | ||
cat xx.m | octave --no-gui | ||
if [ $? -ne 0 ]; then continue; fi | ||
|
||
done | ||
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# ATerm Grammar | ||
|
||
A simple ANTLR4 grammar for [aterm](https://homepages.cwi.nl/~daybuild/daily-books/technology/aterm-guide/aterm-guide.html#section.aterm-introduction). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
BSD License | ||
Copyright (c) 2024, Tom Everett | ||
All rights reserved. | ||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions | ||
are met: | ||
1. Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
2. Redistributions in binary form must reproduce the above copyright | ||
notice, this list of conditions and the following disclaimer in the | ||
documentation and/or other materials provided with the distribution. | ||
3. Neither the name of Tom Everett nor the names of its contributors | ||
may be used to endorse or promote products derived from this software | ||
without specific prior written permission. | ||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
// $antlr-format alignTrailingComments true, columnLimit 150, minEmptyLines 1, maxEmptyLinesToKeep 1, reflowComments false, useTab false | ||
// $antlr-format allowShortRulesOnASingleLine false, allowShortBlocksOnASingleLine true, alignSemicolons hanging, alignColons hanging | ||
|
||
grammar aterm; | ||
|
||
aterm_ : term_ EOF; | ||
|
||
term_: ( afun | num_ | function_ |placeholder | list_ ) annotation?; | ||
|
||
afun : STRING_ | ID_; | ||
|
||
num_: INT_ | REAL_; | ||
|
||
function_: afun args_?; | ||
|
||
args_: '(' termlist? ')'; | ||
|
||
placeholder: '<' term_ '>'; | ||
|
||
list_: '[' termlist? ']'; | ||
|
||
termlist: term_ (',' term_)* ; | ||
|
||
annotation: '{' termlist? '}'; | ||
|
||
REAL_: INT_ '.' NAT EXP_?; | ||
|
||
INT_: ('+' |'-')? NAT; | ||
|
||
fragment NAT: [0-9]+; | ||
|
||
fragment EXP_: (('e'|'E') INT_) | 'ε'; | ||
|
||
ID_: [a-zA-Z][a-zA-Z0-9]*; | ||
|
||
STRING_: '"' ~'"'* '"'; | ||
|
||
WS | ||
: [ \r\n\t]+ -> skip | ||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<desc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../_scripts/desc.xsd"> | ||
<targets>CSharp;Cpp;Dart;Java</targets> | ||
</desc> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
"yellow" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[1,2,"abc"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[f,g([1,2]),x] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<int> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<[3]> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<f(<int>,<real>)> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
exam(<appl(<term>,9)>,<int>,<str>) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
exam(<blob>) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
exam(pair(yellow,9),10, "any") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-0.7E34 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
3.14 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
f(a,b) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
"test!"(1,2.1,"Hello world!") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<artifactId>aterm</artifactId> | ||
<packaging>jar</packaging> | ||
<name>khubla.com ATerm grammar</name> | ||
<parent> | ||
<groupId>org.antlr.grammars</groupId> | ||
<artifactId>grammarsv4</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.antlr</groupId> | ||
<artifactId>antlr4-maven-plugin</artifactId> | ||
<version>${antlr.version}</version> | ||
<configuration> | ||
<sourceDirectory>${basedir}</sourceDirectory> | ||
<includes> | ||
<include>aterm.g4</include> | ||
</includes> | ||
<visitor>true</visitor> | ||
<listener>true</listener> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>antlr4</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<groupId>com.khubla.antlr</groupId> | ||
<artifactId>antlr4test-maven-plugin</artifactId> | ||
<version>${antlr4test-maven-plugin.version}</version> | ||
<configuration> | ||
<verbose>false</verbose> | ||
<showTree>false</showTree> | ||
<entryPoint>aterm_</entryPoint> | ||
<grammarName>aterm</grammarName> | ||
<packageName></packageName> | ||
<exampleFiles>examples/</exampleFiles> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>test</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import {Parser, Lexer, Token, TokenStream, ParserRuleContext} from "antlr4ng"; | ||
import { bnfParser } from './bnfParser.js'; | ||
import { bnfLexer } from './bnfLexer.js'; | ||
|
||
export default abstract class bnfParserBase extends Parser { | ||
|
||
constructor(input: TokenStream) { | ||
super(input); | ||
} | ||
|
||
NotNL(): boolean | ||
{ | ||
var i = 1; | ||
var c = this.inputStream.LT(i); | ||
var v = c.type != bnfParser.NL; | ||
return v; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"""The script transforms the grammar to fit for the c++ target """ | ||
import sys | ||
import re | ||
import shutil | ||
from glob import glob | ||
from pathlib import Path | ||
|
||
def main(): | ||
"""Executes the script.""" | ||
for file in glob("./*.g4"): | ||
transform_grammar(file) | ||
|
||
def transform_grammar(file_path): | ||
"""Transforms the grammar to fit for the target""" | ||
print("Altering " + file_path) | ||
if not Path(file_path).is_file: | ||
print(f"Could not find file: {file_path}") | ||
sys.exit(1) | ||
|
||
shutil.move(file_path, file_path + ".bak") | ||
with open(file_path + ".bak",'r', encoding="utf-8") as input_file: | ||
with open(file_path, 'w', encoding="utf-8") as output_file: | ||
for line in input_file: | ||
line = re.sub(r"(\/\/ Insert here @header for C\+\+ parser\.)",\ | ||
'@header {import bnfParserBase from "./bnfParserBase.js"}', line) | ||
output_file.write(line) | ||
|
||
print("Writing ...") | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using Antlr4.Runtime; | ||
using System.IO; | ||
|
||
public abstract class bnfParserBase : Parser { | ||
private readonly ITokenStream _input; | ||
|
||
protected bnfParserBase(ITokenStream input, TextWriter output, TextWriter errorOutput) | ||
: base(input, output, errorOutput) | ||
{ | ||
_input = input; | ||
} | ||
|
||
public bool NotNL() | ||
{ | ||
int i = 1; | ||
IToken c = (this.InputStream as CommonTokenStream).LT(i); | ||
var v = c.Type != bnfParser.NL; | ||
return v; | ||
} | ||
|
||
public bool NotAssign() | ||
{ | ||
int i = 1; | ||
IToken c = (this.InputStream as CommonTokenStream).LT(i); | ||
while (c != null && c.Type == bnfLexer.WS) | ||
c = (this.InputStream as CommonTokenStream).LT(++i); | ||
var v = !(c.Type == bnfParser.ASSIGN1 | ||
| c.Type == bnfParser.ASSIGN2 | ||
| c.Type == bnfParser.ASSIGN3 | ||
| c.Type == bnfParser.ASSIGN4); | ||
return v; | ||
} | ||
|
||
public bool NotLhs() | ||
{ | ||
int i = 1; | ||
IToken c = (this.InputStream as CommonTokenStream).LT(i); | ||
while (c != null && c.Type == bnfLexer.WS) | ||
c = (this.InputStream as CommonTokenStream).LT(++i); | ||
if (c != null && c.Type != bnfLexer.X1) | ||
return true; | ||
// '<' | ||
for (;;) | ||
{ | ||
while (c != null && c.Type == bnfLexer.WS) | ||
c = (this.InputStream as CommonTokenStream).LT(++i); | ||
if (c != null && c.Type != bnfLexer.ID && c.Type != bnfLexer.X2) | ||
return true; | ||
// ID | ||
if (c == null) return true; | ||
if (c.Type == bnfLexer.X2) | ||
break; | ||
} | ||
// '>' | ||
while (c != null && c.Type == bnfLexer.WS) | ||
c = (this.InputStream as CommonTokenStream).LT(++i); | ||
if (c.Type == bnfLexer.ASSIGN1) return false; | ||
if (c.Type == bnfLexer.ASSIGN2) return false; | ||
if (c.Type == bnfLexer.ASSIGN3) return false; | ||
if (c.Type == bnfLexer.ASSIGN4) return false; | ||
return true; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import 'package:antlr4/antlr4.dart'; | ||
import 'bnfLexer.dart'; | ||
|
||
abstract class bnfParserBase extends Parser { | ||
final TokenStream _input; | ||
|
||
bnfParserBase(this._input) | ||
: super(_input); | ||
|
||
bool NotNL() { | ||
int i = 1; | ||
var c = (_input as CommonTokenStream).LT(i); | ||
bool v = c?.type != bnfLexer.TOKEN_NL; | ||
return v; | ||
} | ||
|
||
bool notAssign() { | ||
int i = 1; | ||
var c = (_input as CommonTokenStream).LT(i); | ||
while (c != null && c.type == bnfLexer.TOKEN_WS) { | ||
c = (_input as CommonTokenStream).LT(++i); | ||
} | ||
bool v = !(c?.type == bnfLexer.TOKEN_ASSIGN1 || | ||
c?.type == bnfLexer.TOKEN_ASSIGN2 || | ||
c?.type == bnfLexer.TOKEN_ASSIGN3 || | ||
c?.type == bnfLexer.TOKEN_ASSIGN4); | ||
return v; | ||
} | ||
|
||
bool notLhs() { | ||
int i = 1; | ||
var c = (_input as CommonTokenStream).LT(i); | ||
while (c != null && c.type == bnfLexer.TOKEN_WS) { | ||
c = (_input as CommonTokenStream).LT(++i); | ||
} | ||
if (c != null && c.type != bnfLexer.TOKEN_X1) { | ||
return true; | ||
} | ||
// '<' | ||
while (true) { | ||
while (c != null && c.type == bnfLexer.TOKEN_WS) { | ||
c = (_input as CommonTokenStream).LT(++i); | ||
} | ||
if (c != null && c.type != bnfLexer.TOKEN_ID && c.type != bnfLexer.TOKEN_X2) { | ||
return true; | ||
} | ||
// ID | ||
if (c == null) { | ||
return true; | ||
} | ||
if (c.type == bnfLexer.TOKEN_X2) { | ||
break; | ||
} | ||
} | ||
// '>' | ||
while (c != null && c.type == bnfLexer.TOKEN_WS) { | ||
c = (_input as CommonTokenStream).LT(++i); | ||
} | ||
if (c?.type == bnfLexer.TOKEN_ASSIGN1) return false; | ||
if (c?.type == bnfLexer.TOKEN_ASSIGN2) return false; | ||
if (c?.type == bnfLexer.TOKEN_ASSIGN3) return false; | ||
if (c?.type == bnfLexer.TOKEN_ASSIGN4) return false; | ||
return true; | ||
} | ||
} |
Oops, something went wrong.