Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am writing a parser in python that will parse the given grammar into an Abstract Syntax Tree. I already have a scanner built using

I am writing a parser in python that will parse the given grammar into an Abstract Syntax Tree. I already have a scanner built using regular expressions. I'm having a lot of trouble actually starting the parser portion. My main issue is how do I tap into my token list after the file has been read, additionally I'm not really sure how to code the grammar so that it will be output in AST. Can anyone help get me started and on the right track? Thanks(Python code and Grammar below).

import re import sys f = open("scannertestdata.txt","r") string = f.read() #string = sys.stdin.read() use later when submitting scanner=re.Scanner([ (r"(var|fun|if|else|return|read|write|not|or|and)", lambda scanner,token:("KEYWORD: ", token)), (r"\d+\.\d*",lambda scanner,token:("REAL",token)), (r"\d+",lambda scanner,token:("INTEGER", token)), (r"[a-zA-Z0-9]+",lambda scanner,token:("IDENTIFIER", token)), (r"(\(|\)|{|}|,|\+|\-|\*|/|%|:=|!=|=|)",lambda scanner,token:("PUNCTUATION", token)), (r"(#t|#f)+",lambda scanner,token:("BOOLEAN", token)), (r"\s+",None),# Skips white space (r'.', lambda scanner,token:None)#Skips invalid token ]) results, remainder=scanner.scan(string) print(results[0] )image text in transcribed 

program id bool literal int literal real literal constant cmds BOOLEAN> KINTEGER> CREAL> bool_literal int literal | real_literal any IDENTIFIER token assignment cmd cmds id := expr ; Expressions are based on Pascal grammar: https:/www.cs.utexas.edu/usersovak/grammar.html simple-expr [rel-op simple-expr] expr simple-expr term factor [sign op 1 term faddop term constant l id I not-op factor | ( expr) expr { , expr) not factor mul op factor exprs not op sign_op add-op mul-op rel op not, or, and were scanned as keywords, but they are used as operators program id bool literal int literal real literal constant cmds BOOLEAN> KINTEGER> CREAL> bool_literal int literal | real_literal any IDENTIFIER token assignment cmd cmds id := expr ; Expressions are based on Pascal grammar: https:/www.cs.utexas.edu/usersovak/grammar.html simple-expr [rel-op simple-expr] expr simple-expr term factor [sign op 1 term faddop term constant l id I not-op factor | ( expr) expr { , expr) not factor mul op factor exprs not op sign_op add-op mul-op rel op not, or, and were scanned as keywords, but they are used as operators

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Informix Database Administrators Survival Guide

Authors: Joe Lumbley

1st Edition

0131243144, 978-0131243149

More Books

Students also viewed these Databases questions

Question

=+1. What is the role of the financial system?

Answered: 1 week ago