actually commit TP2
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
//define a lexical analyser called Example1
|
||||
|
||||
lexer grammar Example1;
|
||||
|
||||
OP : '+'| '*' | '-' | '/' ;
|
||||
DIGIT : [0-9] ;
|
||||
LETTER : [A-Za-z] ;
|
||||
ID : LETTER (LETTER | DIGIT)* ; // match idents
|
||||
WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines
|
||||
@@ -0,0 +1,17 @@
|
||||
MAINFILE = main
|
||||
PACKAGE = Example1
|
||||
|
||||
ifndef ANTLR4
|
||||
$(error variable ANTLR4 is not set)
|
||||
endif
|
||||
|
||||
default: $(PACKAGE).py
|
||||
|
||||
$(PACKAGE).py: $(PACKAGE).g4
|
||||
$(ANTLR4) $^ -Dlanguage=Python3
|
||||
|
||||
run: $(MAINFILE).py $(PACKAGE)*.py
|
||||
python3 $<
|
||||
|
||||
clean:
|
||||
rm -rf *~ $(PACKAGE)*.py $(PACKAGE)*.pyc *.interp *.tokens __pycache*
|
||||
@@ -0,0 +1,25 @@
|
||||
from antlr4 import InputStream
|
||||
from antlr4 import CommonTokenStream
|
||||
from Example1 import Example1
|
||||
|
||||
import sys
|
||||
|
||||
|
||||
def main():
|
||||
# InputStream reads characters (from stdin in our case)
|
||||
input_stream = InputStream(sys.stdin.read())
|
||||
# The generated lexer groups characters into Tokens ...
|
||||
lexer = Example1(input_stream)
|
||||
# ... and the stream of Tokens is managed by the TokenStream.
|
||||
stream = CommonTokenStream(lexer)
|
||||
|
||||
# Display the token stream
|
||||
stream.fill() # needed to get stream.tokens (otherwise lazily filled-in)
|
||||
for t in stream.tokens:
|
||||
print(t)
|
||||
print("Finished")
|
||||
|
||||
|
||||
# warns pb if py file is included in others
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,17 @@
|
||||
//define a tiny grammar for arith expressions with identifiers
|
||||
|
||||
grammar Example2;
|
||||
|
||||
full_expr: expr ';' EOF ;
|
||||
|
||||
expr: expr OP expr
|
||||
| ID {print('oh an id : '+$ID.text)}
|
||||
| INT
|
||||
;
|
||||
|
||||
OP : '+'| '*' | '-' | '/' ;
|
||||
|
||||
|
||||
INT : '0'..'9'+ ;
|
||||
ID : ('a'..'z'|'A'..'Z')+ ;
|
||||
WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines
|
||||
@@ -0,0 +1,17 @@
|
||||
MAINFILE = main
|
||||
PACKAGE = Example2
|
||||
|
||||
ifndef ANTLR4
|
||||
$(error variable ANTLR4 is not set)
|
||||
endif
|
||||
|
||||
default: $(PACKAGE)Parser.py
|
||||
|
||||
$(PACKAGE)Parser.py: $(PACKAGE).g4
|
||||
$(ANTLR4) $^ -Dlanguage=Python3
|
||||
|
||||
run: $(MAINFILE).py $(PACKAGE)Parser.py
|
||||
python3 $<
|
||||
|
||||
clean:
|
||||
rm -rf *~ $(PACKAGE)*.py $(PACKAGE)*.pyc *.interp *.tokens __pycache*
|
||||
@@ -0,0 +1,22 @@
|
||||
from antlr4 import InputStream
|
||||
from antlr4 import CommonTokenStream
|
||||
|
||||
# include to use the generated lexer and parser
|
||||
from Example2Lexer import Example2Lexer
|
||||
from Example2Parser import Example2Parser
|
||||
|
||||
import sys
|
||||
|
||||
|
||||
def main():
|
||||
input_stream = InputStream(sys.stdin.read())
|
||||
lexer = Example2Lexer(input_stream)
|
||||
stream = CommonTokenStream(lexer)
|
||||
parser = Example2Parser(stream)
|
||||
parser.full_expr() # We want to recognize full_expr in the grammar Example2
|
||||
print("Finished")
|
||||
|
||||
|
||||
# warns pb if py file is included in others
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,18 @@
|
||||
//define a tiny grammar with attributes for arith expressions with identifiers
|
||||
|
||||
grammar Example3;
|
||||
|
||||
full_expr: e0=expr ';' EOF {print($e0.text + " has " + str($e0.count) + " operators!")} ;
|
||||
|
||||
expr returns [int count]: // expr has an integer attribute called count
|
||||
| e0=expr OP e1=expr {$count = $e0.count + $e1.count + 1} // name sub-parts and access their attributes
|
||||
| ID {$count = 0}
|
||||
| INT {$count = 0}
|
||||
;
|
||||
|
||||
OP : '+'| '*' | '-' | '/' ;
|
||||
|
||||
|
||||
INT : '0'..'9'+ ;
|
||||
ID : ('a'..'z'|'A'..'Z')+ ;
|
||||
WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines
|
||||
@@ -0,0 +1,17 @@
|
||||
MAINFILE = main
|
||||
PACKAGE = Example3
|
||||
|
||||
ifndef ANTLR4
|
||||
$(error variable ANTLR4 is not set)
|
||||
endif
|
||||
|
||||
default: $(PACKAGE)Parser.py
|
||||
|
||||
$(PACKAGE)Parser.py: $(PACKAGE).g4
|
||||
$(ANTLR4) $^ -Dlanguage=Python3
|
||||
|
||||
run: $(MAINFILE).py $(PACKAGE)Parser.py
|
||||
python3 $<
|
||||
|
||||
clean:
|
||||
rm -rf *~ $(PACKAGE)*.py $(PACKAGE)*.pyc *.interp *.tokens __pycache*
|
||||
@@ -0,0 +1,22 @@
|
||||
from antlr4 import InputStream
|
||||
from antlr4 import CommonTokenStream
|
||||
|
||||
# include to use the generated lexer and parser
|
||||
from Example3Lexer import Example3Lexer
|
||||
from Example3Parser import Example3Parser
|
||||
|
||||
import sys
|
||||
|
||||
|
||||
def main():
|
||||
input_stream = InputStream(sys.stdin.read())
|
||||
lexer = Example3Lexer(input_stream)
|
||||
stream = CommonTokenStream(lexer)
|
||||
parser = Example3Parser(stream)
|
||||
parser.full_expr()
|
||||
print("Finished")
|
||||
|
||||
|
||||
# warns pb if py file is included in others
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user