Lmnl-ANTLR.py

From LMNLWiki

(back to the LMNL Parser Experiment page LMNL Parser Experiment

# Simple Python script to parse an LMNL text, in a file called 'example-lmnl-text.txt'
# using the generated ANTLR parser.  Run from the command line using:
# python lmnl-ANTLR.py

from sys import stdin, stdout, stderr
import os, sys
import antlr3
from LMNLLexer import LMNLLexer
from LMNLParser import LMNLParser
from LMNLLexer import EOF

   
def antlrParseLMNLDocument():
    lmnlDocument = antlr3.ANTLRFileStream('example-lmnl-text.txt')
    lexer = LMNLLexer(lmnlDocument)
    tokens = antlr3.CommonTokenStream(lexer)
    parser = LMNLParser(tokens)
    print "Processing"
    parser.lmnlDocument()

# If we're running from the command line, parse the document:
if __name__ == '__main__':
    antlrParseLMNLDocument()