def main(): files = get_filenames() for (input_file, tokens_file, compiled_file) in files: print("Parsing ", input_file, "into tokens file ", tokens_file, "and compiled file", compiled_file) tokens = get_tokens(input_file) ast = parse(input_file) pretty_dump(tokens, tokens_file) pretty_dump(ast, compiled_file)
def main(): #Check if we got the filename by argument if(len(sys.argv) == 3): try: file = open(sys.argv[1], "r") except: print('File ' + str(sys.argv[1]) + ' not found') sys.exit() code = file.read() file.close() else: print('Missing parameter') sys.exit() #Parse parser.parse(code, tracking = True) #Save the quadruples in a file export_quadruples(sys.argv[2]) export_constants('c_'+sys.argv[2])
def parser_repl(): while True: try: s = input('calc > ') except EOFError: break if not s: continue result = parser.parse(s) print(result)
def cli(filepath, verbose): with open(filepath, 'r') as f: read = f.read() """ if verbose: click.echo('Showing tokens...') lexer.input(read) for tok in lexer: click.echo(tok) """ if verbose: click.echo('Showing parse...') click.echo(parser.parse(read))
def analyze(string: str): parse = parser.parse(lexer.lex(string)) assert parse is not None # make sure the program is grammatically valid return semantics.analyze(parse) is not None
def parse(string: str) -> bool: return isinstance(parser.parse(lexer.lex(string)), Program)
self.children[1].verify() @addToClass(AST.WhileNode) def verify(self): self.children[0].verify() self.children[1].verify() @addToClass(AST.IfNode) def verify(self): self.children[0].verify() self.children[1].verify() @addToClass(AST.CompNode) def verify(self): for c in self.children: c.verify() if __name__ == '__main__': from compiler import parser from compiler.parser import parse import sys prog = open(sys.argv[1]).read() ast = parse(prog) ast.verify() if len(errors) == 0: print("Code is semantically valid") for error in errors: print(error)
from compiler.parser import parse argparser = argparse.ArgumentParser(description="Compiler 37") argparser.add_argument("files", metavar="FILE", type=str, nargs="+", help="Input files") argparser.add_argument( "-o", "--output", dest="out", type=str, default="-", help="Output assembly to a file", ) args = argparser.parse_args() c = Compile() for file in args.files: with open(file, "r") as f: c.add_file(parse(f.read())) asm = str(c.finish()) if args.out == "-": print(asm) else: with open(args.out, "w") as f: f.write(asm)
def compile_code(source_code): tree = parse(source_code) return generate_code(tree)
import sys from compiler.lexer import lex from compiler.parser import parse from compiler.semantics import analyze from compiler.codegen import to_ir def display(num, line): instruction, source1, source2, dest = line print( f'{num + 1:<4}{instruction:10}{source1 or "":10}{source2 or "":10}{dest or ""}' ) if __name__ == '__main__': with open(sys.argv[1], 'r') as f: [ display(i, line) for i, line in enumerate(to_ir(analyze(parse(lex(f.read()))))) ]
def to_ir(string: str): analyzed = semantics.analyze(parser.parse(lexer.lex(string))) assert analyzed is not None # make sure the program passes the semantic analysis return codegen.to_ir(analyzed)
def main(args): parser = argparse.ArgumentParser( description='Compiler for the Caesar programming language') parser.add_argument('--repl', action='store_true', default=False, help='start the compiler as a REPL interpreter') parser.add_argument('--bin', '-b', nargs='?', metavar='FILE_NAME', action='store', default=False, help='name of binary to emit') parser.add_argument('--dylib', '-d', nargs='?', metavar='FILE_NAME', action='store', default=False, help='name of dynamic lib to emit') parser.add_argument('--lib', '-l', nargs='?', metavar='FILE_NAME', action='store', default=False, help='name of static lib to emit') parser.add_argument('--obj', '-o', action='store_true', default=False, help='generate object file(s) as output') parser.add_argument('--asm', '-s', nargs='*', metavar='INPUT_FILE_NAME', action='store', default=False, help='generate assembly file(s) as output') parser.add_argument( '--run', '-r', nargs='*', metavar='PROGRAM_ARGS', action='store', default=False, help='execute the program immediately after compilation') parser.add_argument('--sources', '-f', nargs='*', metavar='INPUT_FILE_NAME', action='store', default=[], help='file names to compile') parser.add_argument('moreSources', nargs='*', metavar='INPUT_FILE_NAME', action='store', default=[], help='file names to compile') args = parser.parse_args(args[1:]) if args.asm != False: args.sources.extend(args.asm) args.asm = True args.sources.extend(args.moreSources) if not args.sources: if args.run != False: args.sources = args.run[:1] args.run = args.run[1:] elif args.bin != False: args.sources = args.bin and [args.bin] args.bin = None elif args.lib != False: args.sources = args.lib and [args.lib] args.lib = None elif args.dylib != False: args.sources = args.dylib and [args.dylib] args.dylib = None binFileName = None if args.bin != False: if args.bin == None: if len(args.sources) != 1: print('Error: cannot infer output file name for binary. ' + 'Please provide a file name as an argument to `--bin`') exit(1) args.bin = os.path.splitext(args.sources[0])[0] binFileName = args.bin args.bin = True else: binFileName = '/tmp/caesar_tmp_{}'.format(str(uuid.uuid1())) libFileName = None if args.lib != False: if args.lib == None: if len(args.sources) != 1: print('Error: cannot infer output file name for library. ' + 'Please provide a file name as an argument to `--lib`') exit(1) args.lib = os.path.splitext(args.sources[0])[0] + '.lib' libFileName = args.lib args.lib = True dyLibFileName = None if args.dylib != False: if args.dylib == None: if len(args.sources) != 1: print('Error: cannot infer output file name for DLL. ' + 'Please provide a file name as an argument to `--dylib`') exit(1) args.dylib = os.path.splitext(args.sources[0])[0] + '.dylib' dylibFileName = args.dylib args.dylib = True asmFileNames = None if args.asm != False: asmFileNames = [os.path.splitext(f)[0] + '.asm' for f in args.sources] else: asmFileNames = [ '/tmp/caesar_tmp_{}.asm'.format(str(uuid.uuid1())) for _ in args.sources ] objFileNames = [os.path.splitext(f)[0] + '.o' for f in args.sources] runArgs = args.run or [] args.run = not args.run == False if args.repl: if args.run or args.bin or args.dylib or args.lib or args.obj or args.asm: print( 'Error: the following arguments may not be used in REPL mode:') print(' --run, --bin, --dylib, --lib, --obj, --asm') exit(1) elif args.sources: print('Error: input file names are not allowed in REPL mode') exit(1) elif not (args.run or args.bin or args.dylib or args.lib or args.obj or args.asm): print('Error: no output method selected. Expected one or more of:') print(' --run, --bin, --dylib, --lib, --obj, --asm') exit(1) elif args.run + args.bin + args.dylib + args.lib > 1: print('Error: can only select one of:') print(' --run, --bin, --dylib, --lib') exit(1) elif not args.sources: print('Error: no input files supplied') exit(1) if args.repl: print('Error: REPL option not yet implemented') exit(1) elif args.lib or args.dylib: print( 'Error: output methods `--lib` and `--dylib` are currently unimplemented' ) exit(1) for (i, fileName) in enumerate(args.sources): try: source = SourceFile(fileName) except Exception as e: print(e) exit(1) tok = tokenize(source) ast = parse(source, tok) mod = analyze(ast) generateIR(mod) asm = generateAsm(mod) try: outfile = open(asmFileNames[i], 'w') outfile.write(asm) outfile.close() if args.obj or args.bin or args.lib or args.dylib or args.run: asmExitCode = os.system('nasm -f macho64 {} -o {}'.format( asmFileNames[i], objFileNames[i])) if asmExitCode != 0: exit(1) except Exception as e: print(e) exit(1) try: if not args.asm and (args.obj or args.bin or args.lib or args.dylib or args.run): for f in asmFileNames: os.remove(f) if args.bin or args.run: os.system( 'ld -e _start -macosx_version_min 10.8 -arch x86_64 {} -lc -lSystem -no_pie -o {}' .format(' '.join(objFileNames), binFileName)) if args.run and not args.obj: for f in objFileNames: os.remove(f) if args.run: os.system('{} {}'.format(binFileName, ' '.join(runArgs))) os.remove(binFileName) except Exception as e: print(e) exit(1)
def main(): result = parser.parse(data, lexer=lexer, debug=False) print(result)
#! /usr/bin/env python3 from argparse import ArgumentParser from compiler.scanner import tokenize from compiler.parser import parse from compiler.code_generator import generate if __name__ == "__main__": argParser = ArgumentParser( description='Compile Retro Basic language into bytecode') argParser.add_argument('source', help='Source path') argParser.add_argument('-o', '--output', help='Output path') args = argParser.parse_args() infile = open(args.source) data = infile.read() infile.close() tokens = tokenize(data) parse_tree = parse(tokens) # print(parse_tree) bytecode = ' '.join([str(e) for e in generate(parse_tree)]) if args.output is not None: with open(args.output, 'w') as outfile: outfile.write(bytecode) else: print(bytecode)