程序代写案例-CSCI 435
2021/2/22 CSCI 435 lab 3
csci.viu.ca/~wesselsd/courses/csci435/labs/lab3.html 1/3
CSCI 485: Lab 3 (Spring 2021)
Review and prep:
Lab 3 picks up where lab 2 left off, so you'll need to get your lab2 solution working in order
for your lab3 to work.
Your lab3 git repository will contain skeletal versions of the .lex and .yacc files (lab3.lex and
lab3.yacc) but you'll want to replace these with the code you developed for lab2.
Updates to the language take place regularly over labs 2-4
See the table at the bottom of the overview page for the language updates as they are
released.
Lab 3 exercise:
Lab 3 will focus on adding a variety of new language features to our recognizer, along with
corresponding compile-time checks and revisions to the symbol table.
Objectives:
The lab3 executable still needs to perform the parsing and checking identified for lab 2, but it
will also adds the following:
Typed global variables:
Global variable declarations are a new form of STATEMENT, with syntax
TYPENAME VARNAME SEMI
- type names are int, name, str, facing
- variable names start with a lowercase alphabetic characters, followed by zero or more
digits or lowercase alphabetic characters
- variables cannot be used before being declared, cannot shadow a language keyword
(repeat, start, if, int, etc), and you cannot have two global variables with the same name
- variables can be used anywhere a value of the same type can be used except as the
name field for a create, e.g. name variables can be used in lookups, int/name/facing
variables can be used in actions, etc
Drop the compile-time checking for numeric values being within the map size (now that
we have variables, this will become a run-time check).
A new lookup function is added: face, e.g. face ( Bob )
- returns the ai's current facing (north, south, etc)
- as usual, the name must be declared before being used
String literals are introduced, with form '.........'
- strings currently cannot include a ' or a newline, but can include a %
(so the order in which you handle strings/comments is important)
2021/2/22 CSCI 435 lab 3
csci.viu.ca/~wesselsd/courses/csci435/labs/lab3.html 2/3
Expanded VALUE types: values now also include string literals, variables, and names
- the := operator expects the types of the RHS value and LHS variable match
- the = and >< operators can handle any two values of the same type
- the < and <= operators only handle numeric values (i.e. numbers, integer variables,
and the results of row/col lookups)
- type checking needs to be performed during compilation
Print statements can now display any single value, rather than strictly a name, i.e. PRINT
VALUE SEMI
Assignments are a new form of STATEMENT, with syntax
VARNAME ASSIGNOP VALUE SEMI
- the only assignment operator is currently ":="
% examples, assuming Bob has been created
int abc ;
abc := row ( Bob ) ;
abc := 27 ;
print abc ;
name whoever ;
whoever := Bob ;
str text ;
text := 'hello!' ;
str moretext ;
moretext := text ;
facing mydir ;
mydir := south ;
mydir := face ( Bob ) ;
As always, if an error is detected then an informative error message must be generated, that
clearly identifies the specific offending item. For example, if a name is used in two creates
then the error message(s) should identify both the nature of the error (a redeclaration) and
the name that was used.
Tips:
(1) You can either maintain a single symbol table for both AI and vars, or create seperate
tables for each. Your handling of variables will be much like the handling of AI
- on a var declaration: make sure it isn't redeclaring an existing variable, then add it to the
table and record its type.
- on a var use: make sure it has been previously declared, and that its type is appropriate for
the way it is being used.
(2) With our yacc rules of the form
nontermA: tokens and nonterminals { ... };
we can set node values for the LHS nonterminal (e.g. nonTermA) based on the values we
looked up on the RHS (e.g. with $1)
The syntax to set fields for the LHS nonterminal is $$ (i.e. $ on the right instead of
an index position).
2021/2/22 CSCI 435 lab 3
csci.viu.ca/~wesselsd/courses/csci435/labs/lab3.html 3/3
/* basic example: suppose we had vars that could be ints or strings,
* we'll store a type value of 0 for ints, 1 for strings,
* and our equal operator can see if the types match,
* (assuming we've added a datatype field to our info struct) */
var: NUMBER
{
$$ = 0;
}
;
var: STRING
{
$$ = 1;
}
;
compare: var EQ var
{
if ($1 != $2) {
/* type mismatch detected */
}
}
;
Note that yacc fills in the node values "bottom up" when parsing the tree, so the types do get
correctly determined.
Testing:
As with lab2, when evaluating your programs, they'll be run against a suite of valid Wanderful
programs and a suite of invalid Wanderful programs, using different subsets and
combinations of the language features.
I strongly recommend creating a collection of Wanderful test cases and programs. While your
.lex and .yacc code must be strictly your own work, I have no objection to students sharing
interesting Wanderful programs for use as test cases.

欢迎咨询51作业君
51作业君 51作业君

Email:51zuoyejun

@gmail.com

添加客服微信: ITCSdaixie