Skip to content

Instantly share code, notes, and snippets.

@nmz787

nmz787/Lexer.g4

Created May 19, 2020
Embed
What would you like to do?
Run with:
lexer grammar Lexer;
fragment WS: [\t ]+;
fragment NL: WS? ('\r'* '\n')+;
COLON_ATTRIBUTES: ('KeyId' |
'first key' |
'key' |
'key (interesting)' |
'key(# of 1second)' |
'key.subkey')+ -> mode(COLON_ATTRIBUTES_MODE);
mode COLON_ATTRIBUTES_MODE; //key: value is whatever might come before the newline
COLON_ATTRIBUTES_INTERMEDIATE: (' '|'\t')* ':' (' '|'\t')* -> channel(HIDDEN);
COLON_ATTRIBUTES_DATA: ~[\r\n]+;
COLON_ATTRIBUTES_END: NL -> channel(HIDDEN), mode(DEFAULT_MODE);
from antlr4 import *
if __name__ is not None and "." in __name__:
from .Parser import Parser
else:
from Parser import Parser
# This class defines a complete listener for a parse tree produced by Parser.
class Listener(ParseTreeListener):
# Enter a parse tree produced by Parser#whole_log.
def enterWhole_log(self, ctx:Parser.Whole_logContext):
pass
# Exit a parse tree produced by Parser#whole_log.
def exitWhole_log(self, ctx:Parser.Whole_logContext):
pass
# Enter a parse tree produced by Parser#colon_attributes.
def enterColon_attributes(self, ctx:Parser.Colon_attributesContext):
print([c.getText() for c in ctx.getChildren()])
pass
# Exit a parse tree produced by Parser#colon_attributes.
def exitColon_attributes(self, ctx:Parser.Colon_attributesContext):
pass
del Parser
parser grammar Parser;
options {
tokenVocab=Lexer;
}
whole_log: colon_attributes+;
colon_attributes: COLON_ATTRIBUTES COLON_ATTRIBUTES_INTERMEDIATE COLON_ATTRIBUTES_DATA COLON_ATTRIBUTES_END;
"""
"C:\Program Files\JetBrains\PyCharm Community Edition 2020.1\jbr\bin\java.exe" -Xmx500M -cp "C:\Users\nmz787\Downloads\antlr-4.8-complete.jar" org.antlr.v4.Tool Lexer.g4 -Dlanguage=Python3
"C:\Program Files\JetBrains\PyCharm Community Edition 2020.1\jbr\bin\java.exe" -Xmx500M -cp "C:\Users\nmz787\Downloads\antlr-4.8-complete.jar" org.antlr.v4.Tool Parser.g4 -Dlanguage=Python3
"""
from antlr4 import CommonTokenStream, ParseTreeWalker, InputStream
inp = InputStream("""key: value
""")
from Lexer import Lexer
from Parser import Parser
from Listener import Listener
lexer = Lexer(inp)
stream = CommonTokenStream(lexer)
parser = Parser(stream)
printer = Listener()
# parser.removeErrorListeners()
# lexer.removeErrorListeners()
tree = parser.whole_log()
walker = ParseTreeWalker()
walker.walk(printer, tree)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment