How JustAnswer Works:
  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.
Ask Mr. Gregory White Your Own Question
Mr. Gregory White
Mr. Gregory White, Professor
Category: Homework
Satisfied Customers: 5240
Experience:  M.A., M.S. Education / Educational Administration
46856550
Type Your Homework Question Here...
Mr. Gregory White is online now
A new question is answered every 9 seconds

Project #1: Compiler Front End — #1a: Lexer Introduction The

Customer Question

Project #1: Compiler Front End — #1a: Lexer
Introduction
The objective of this assignment is to write a lexer for the A2 programming language.
Guidelines
The A2 Lexcon is given, below. Recall that a lexer is built via a DFA. The objective is to create a lexer
with a next_token() function so that a parser can call this lexer function to get the next token from the
input character stream. In addition to the Lexicon (DFA grammar) shown below, it is to be assumed that
whitespace delimits tokens, but is itself not a token; meaning that next_token() doesn't return any
whitespace, and that encountering a whitespace char will terminate a token (e.g., an identifier). The lexer must
be hand-built, rather than built by a lexer generator program (e.g., LEX).
The lexer may be written in one of these major programming languages: C, C++, Java, Python.
To test your lexer's next_token() function, write a main program which reads an A2 program file and
outputs a file of the same name with a “.tok” extension.
Each lexer is to be built by a team (of either one or two students).
Each team is to provide a set of 5 sample A2 programs, each of which is a test case for their lexer. Note
that your lexer may be run with other students' lexer sample A2 programs.
Submission
Create a folder named “p1-lexer-” followed by your last name, hyphen, and your first initial. For teams,
use the alphabetically first name. Put your source code and the 5 sample A2 program files in this folder. Your
main source code file, i.e. the tester, should be named “a2-lexer”. Add a README.txt file which includes the
course, the team members' names, and a brief description of how the source is compiled and/or run. Name each
of your sample A2 program files “a2-d-”followed by your last name, hyphen, and your first initial (so that each
of the classes sample A2 programs is distinguishable. Add a test run output file for each sample A2 program
file, suitably named. Include no compiled or executable files.
Create a .zip file of your folder and email it to the instructor by 11pm of October 14.
A2 Lexicon
comment = "//" .* // Treat as whitespace. IE, same as for C, C++, and Java.
id = LU1 LUTR * // identifier.
LUTR = LU1 | [0..9]
LU1 = '_' | [a..zA..Z]
string = '"' .* '"' // Cannot contain a double-quote char.
num = sign ? int [ '.' int ] ? // integer or float
sign = plus | minus
int = [0..9] [0..9] * // integer.
// Multi-char operators
opeq = "=="
opne = "!="
ople = "="
// Keywords
kwdelse = "else"
kwdelseif = "elseif"
kwdfcn = "fcn"
kwdif = "if"
kwdmain = "main"
kwdprog = "prog"
kwdreturn = "return"
kwdvars = "vars"
kwdwhile = "while"
// Paired delimeters
angle1 = ''
brace1 = '{'
brace2 = '}'
bracket1 = '['
bracket2 = ']'
parens1 = '('
parens2 = ')'
// Unpaired delimiters
comma = ','
semi = ';'
// Single-char operators
equal = '='
aster = '*'
slash = '/'
caret = '^'
plus = '+'
minus = '-'
// Sigils
at = '@'
hash = '#'
usd = '$'
PGM = kwdprog VARSECT FCNDEFS MAIN
MAIN = kwdmain BLOCK
BLOCK = brace1 VARSECT STMTS brace2
VARSECT = kwdvars parens1 VARLIST parens2 | eps
VARLIST = VARDEF VARLIST | eps
VARDEF = NVARDEF | SVARDEF | AVARDEF
NVARDEF = NVARID equal EXPR semi | NVARID semi
SVARDEF = SVARID equal EXPR semi | SVARID semi
AVARDEF = AVARID bracket1 int bracket2 semi
FCNDEFS = FCNDEF FCNDEFS | eps
FCNDEF = kwdfcn FCNID PARMLIST BLOCK
PARMLIST = parens1 IDLIST parens2
IDLIST = id comma IDLIST | eps
STMTS = STMT semi STMTS | eps
STMT = STASGN | STCALL | STIF } STWHILE | STPRINT | STRTN
STASGN = LVAL equal EXPR
LVAL = NVARID | SVARID | AVARID | ARELT
ARELT = AVARID bracket1 EXPR bracket2
STCALL = FCALL
FCALL = FCNID parens1 ARGLIST parens2
ARGLIST = EXPR comma ARGLIST | eps
STIF = kwdif PEXPR BLOCK ELSEPART
ELSEPART = kwdelseif PEXPR BLOCK ELSEPART
ELSEPART = kwdelse BLOCK | eps
STWHILE = kwdwhile PEXPR BLOCK
STPRINT = kprint parens1 PRNLIST parens2
PRNLIST = EXPR comma PRNLIST | eps
STRTN = kwdreturn EXPR
PEXPR = parens1 EXPR parens2
EXPR = EXPR OPREL RTERM | RTERM
RTERM = RTERM OPADD TERM | TERM
TERM = TERM OPMUL FACT | FACT
FACT = num | string | LVAL | FCALL | PEXPR
OPREL = opeq | opne | LTHAN | ople | opge | GTHAN
LTHAN = angle1
GTHAN = angle2
OPADD = plus | minus
OPMUL = aster | slash | caret
// Needed to disambiguate id-started STMT and LVAL rules.
AVARID = at id // array var name
NVARID = usd id // num var name
SVARID = hash id // string var name
FCNID = id // function name
Submitted: 1 year ago.
Category: Homework
Customer: replied 1 year ago.
Please see the attached file
Expert:  Mr. Gregory White replied 1 year ago.
THIS ANSWER IS LOCKED!

You need to spend $3 to view this post. Add Funds to your account and buy credits.