首页
网站开发
桌面应用
管理软件
微信开发
App开发
嵌入式软件
工具软件
数据采集与分析
其他
首页
>
> 详细
讲解data编程课程、讲解Java语言程序、Java程序辅导 辅导R语言程序|辅导R语言程序
项目预算:
开发周期:
发布时间:
要求地区:
UCA - HUAT Formal Languages and Compilation - 2020/2021
Compiler - Based on a similar lab work in Caen University
Antlr, Calculator and MVaP language
The objectives of the lab work (on machine) :
1. to be familiar with a lexical and syntactic analyser tool. We have choosen the ANTLRtool,
which uses Java language.
Your friend is the ANTLRdocumentation : https ://github.com/antlr/antlr4/blob/master/doc/index.md
2. In a second part, you will have to design a programming language dealing with variables,
conditional structures and loops. Apart from conditional structures, assignements to
variables and loops, the language is intented to manipulate aritmetical and boolean
expressions. You will use antlr for the lexical, syntactic and code generation. The target
language for the code generation will be the MVaP language, the language of the MVaP
stack machine. The descriptions of the instructions are given in the MVaP-Cheat-Sheet
pdf.
1 Quick start of Antlr
Question. Download the install file install.txt and follow the instructions to install
ANTLR. The installation instructions are for a Gnu/Linux or similar operating systems (Unix,
Mac OS X). I recommend then to read https ://github.com/antlr/antlr4/blob/master/doc/index.md.
The following is an attribute grammar for arithmetical expressions using sum and product.
The first axiom is always start, et every non-terminal should start with a lower case letter.
Tokens always are in upper-case. We end the start axiom with the system token EOF, which
is automatically produced by the lexical analyser after reading the input.
grammar Calculator;
// grammar rules
start
: expr EOF;
expr
: expr ’*’ expr
| expr ’+’ expr
| INT
;
//lexical rules. When something is unmatched, we skip it.
NEWLINE : ’\r’? ’\n’ -> skip;
WS : (’ ’|’\t’)+ -> skip;
INT : (’0’...’9’)+;
UNMATCH : . -> skip;
Question. Explain why this grammar is ambigous.
Question. Put this grammar in a file (say Calculator.g4) and compile (follow the instructions
in the install.txt file).
Question. Use the grun tool to visualise the syntactic tree (see install.txt file for the corresponding
command line). A use case with grun :
grun Calculator ’start’ -gui
Analyse the results with the following expressions :
42
24+ 24- 6
5*8+2* 1
5+2 * 8+ 3
6 *4 / 5+38
Question. We use attribute grammars to evaluate such arithmetical expressions, by associating
each rule with an attribute. We can do the following for the addition rule for instance.
expr returns [int val]
: a=expr ’+’ b=expr {$val=$a.val +$b.val;}
Complete so that the result of the evaluation of the arithmetical expression is printed.
To obtain the text of a token, use the token’s text attribute, and the token’s attribute int to
get the integer value held by the token if the text is a valid numeric string. See https ://github.com/antlr/antlr4/blob/master/doc/actions.md
for the set of token’s and rules attributes,
given by ANTLR and also the syntax for adding your own attributes and retreiving their values.
Question. Analyse the output of the syntactic tree (with grun) and of the result of the
evaluation of the expression 5 + 2 ∗ 8 + 3. Permute now in expr the order in which the rules for
the sum and product are given, and do the same as above with the expression 5 + 2 ∗ 8 + 3.
Do we have same outputs ? Why ?
2 Calculator
The objective is to enrich our calculator language to manipulate complex arithmetical
expressions with variables and types, loops, printing and reading, and conditional structures.
We recommend to use standard techniques to deal with ambiguity. For instance, for
arithmetical expressions, division and product have high priorities on sum and substractions,
with same priorities we evaluate from left to right. We recommend to read again
https ://github.com/antlr/antlr4/blob/master/doc/left-recursion.md to know which rules are
used by default by ANTLR to resolve ambiguity.
We will add more and more rules to our grammar Calculator.g4.
2
Question. Enrich the grammar to include divisions, substractions and parenthesized expressions.
Test with the following expressions (they all evaluate to the same value). We suppose
from now that every expression is ended by a line break, but you can enrich by adding for
instance the possibility to end them also with semi-colons (you encapsulate them in a token
EndOfInstruction).
42
24+24-6
5*8+2*1
6*4/5+38
42+1+2+-3
5*8+2*-1/-1
(5*6*7*11 + 2)/11*5-1008
(5*6*7*11 + 2)/(11*5)
(5*6*7*11 + 2)/11/5
(5*6*7*11 + 2)/(11/5)-1114
Question. We have seen in exercises’ lecture rules for manipulating boolean expressions
with comparisons between arithmetical expressions. Complete these rules to add comparisons
between boolean expressions and add the rules to the Calculator grammar. We recall that
the comparisons operations are == (equality), <> (difference), < (less than), > (greater
than), <= (less or equal than) et >= (greater or equal than), and the operators for boolean
expressions are and, or and not. The rule should also accept parenthesized boolean expressions.
Test with the following expressions.
true and false or true and not true
true or false and false
true or (false or false)
false or (true and not false)
(not true and false) or (true and not false)
false or not true or true and not 42 == 42
false or 42 <= 40
false or (5*6*7*11 + 2)/11*5-1008 <= 5*8+2*-1/-1
50 <> 51
42+1+2+-3 <> 5*8+2*-1/-1
(5*6*7*11 + 2)/11/5 < (5*6*7*11 + 2)/(11/5)-1114
(5*6*7*11 + 2)/11/5 <= (5*6*7*11 + 2)/(11/5)-1114
5*8+2*1 > 6*4/5+38
5*8+2*1 >= 6*4/5+38
42 == (5*6*7*11 + 2)/11/5
3 Code Generation
The goal is to use the MVaP machine to execute the programs written in our language,
and then we need to generate a machine code instead of evaluating directly the expressions.
The target language for the code generation will be the MVaP language.
3
Reminder. If you did not yet install MVaP, please follow the instructions in install.txt.
Read also the MVaP-Cheat-Sheet.pdf file for the set of instructions of the MVaP machine.
Until now, we have associated an integer attribute with each rule to store the result of the
evaluation. For the code generation with target language MVaP, we will instead use a string
accumulator to store the MVaP code.
Question. Modify the grammar so that it generates, for each rule, the associated MVaP
code, instead of evaluating its value.
Tests. Test your parser to check that you really generate the right MVaP, you can use the
expressions above. Run the MVaP generated code to check that the results are correct (see
install.txt on how to run MVaP machine). The tool grun can be quickly too heavy for
testing. Instead, you can use the following to automate the tests :
import java.io.*;
import org.antlr.v4.runtime.*;
public class MainCalculator {
public static void main(String args[]) throws Exception {
CalculatorLexer lex;
if (args.length == 0)
lex = new CalculatorLexer(new ANTLRInputStream(System.in));
else
lex = new CalculatorLexer(new ANTLRFileStream(args[0]));
CommonTokenStream tokens = new CommonTokenStream(lex);
CalculatorParser parser = new CalculatorParser(tokens);
try {
parser.start(); // start the first axiom of the grammar
} catch (RecognitionException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
4 Global Variables
We would like to be able to manipulate global variables and types. We will manipulate
only int, float and bool types. Values by default for variables is 0. In our language, any variable
should be declared before first use. One should be able to assign to variables constants, e.g.,,
x = 4, as well expressions, e.g.,, x = 6 ∗ y (in this latter we are expecting that the value of y
is computed and multiplied with 6 before assigning it to x).
We will follow the C language structure as follows : a program in our language starts with
declarations of variables first and then can do computations. See below, a proposition for the
structure of the file Calculator and a beginning of the rules.
4
grammar Calculator;
@parser::members {
private int _label = 0;
/** generator of label names */
private int nextLabel() { return _label++; }
}
start : calcul EOF;
calcul returns [ String code ]
@init{ $code = new String(); } // we initialise $code, to use it as accumulator
@after{ System.out.println($code); } // we print the MVaP code produced by the parser
: (decl { $code += $decl.code; })*
NEWLINE*
(instruction { $code += $instruction.code; })*
{ $code += " HALT\n"; }
;
EndInstruction
: (NEWLINE | ’;’)+
;
decl returns [ String code ] // declarations of variables
: TYPE IDENTIFIER EndInstruction
{
// to be completed
}
| TYPE IDENTIFIER ’=’ expression EndInstruction
{
// to be completed
}
;
instruction returns [ String code ] // an instruction other than declaration
: expression EndInstruction // boolean or arithmetical expressions
{
//to be completed
}
| assignment EndInstruction // assigning values to variables
{
// to be completed
}
| EndInstruction
{
$code="";
}
;
assignment returns [ String code ] // Assign an expression
5
: IDENTIFIER ’=’ expression
{
//to be completed
}
;
// lexer
TYPE : ’int’ | ’float’ | ’bool’; // three types
IDENTIFIER : ; //to be completed, any sequence of [a-zA-Z0-9_], starting always with [a-zA-Z] or _
Question. To deal with variables, you will probably need symbol tables to be able to decide
whether an identifier has been already used, and also to be able to recover the value of a variable
whenever it is used, in particular know its address in the stack (recall that the declarations are
done at the beginning, and then if you declare variables first in your MVaP code, you are able
to compute their addresses in the stack). To keep track of the tuples (id-variable,type-variable,
adress-in-stack), you can use for instance an HashMap, declared and initialised in the header
@parser : :members.
Modify now the Calculator grammar to generate MVaP code for all the rules we have
written so far (arithmetical and boolean expressions), and for declarations and assignments of
global variables.
Use the test collection to test your grammar. Each of the wanted behaviour has a test file
with a collection of expressions to be tested. From now on, it is better to use the test set to
check whether your proposal corresponds to the wanted behaviour.
Adding Input and Output operations. To ease the tests and the use of our language,
it might interesting to include a mechanism for reading from the system input and writing
on the system output. We recall that the MVaP machine has two dedicated instructions READ
and WRITE. The following rules allow to deal with input and output operations. Add the io
rule to the grammar file and modify also the instruction rule so that one can readln and
println in our language.
io returns [ String code ]
: READ ’(’ IDENTIFIER ’)’
{
int at = // to be completed, adress of the variable $IDENTIFIER.text;
$code=" READ" ;
$code+="\n"; // new line after READ
$code+=" STOREG" +" " + at + "\n";
}
| WRITE ’(’ expression ’)’
{
$code = $expression.code
+ " WRITE" + "\n"
+ " POP" + "\n";
}
;
WRITE : ’println’;
6
READ : ’readln’;
5 Blocks
In order to deal with conditional structures and loops with several instructions, we need
to be able to encapsulate a set of instructions in a block, so that we make a difference between
the instructions to execute inside the conditional structure, and the others outside.
Question. Add to the grammar the possibility to manipulate a block of instructions, encapsulated
between { and }. The beginning of the rule can be the following :
block returns [ String code ]
@init{ $code = new String ()}
: // to be completed
6 While Loops
Let’s enrich our language with while loops. The wanted syntax is the following, with cond
a boolean expression :
while (cond)
instructions
Question. Add the necessary rules to our grammar file. We are expecting that you allow
the two following forms.
while (cond)
// 1 single instruction.
// or a block of instructions
while (cond) {
//several instructions
}
7 Conditional Structures
Let’s now add jumps to our language with the if . . . then else . . . construction. The intended
syntax is the following (cond still a boolean expression) :
if (cond)
instructionthen
else instructionelse
The else is not mandatory. Also, as with the while instruction, the set of instructions
instructionthen and instructionelse might be a single or a block of instructions.
Question. Add the necessary rules for manipulating the if . . . then else . . . instruction.
7
软件开发、广告设计客服
QQ:99515681
邮箱:99515681@qq.com
工作时间:8:00-23:00
微信:codinghelp
热点项目
更多
代写dts207tc、sql编程语言代做
2024-12-25
cs209a代做、java程序设计代写
2024-12-25
cs305程序代做、代写python程序...
2024-12-25
代写csc1001、代做python设计程...
2024-12-24
代写practice test preparatio...
2024-12-24
代写bre2031 – environmental...
2024-12-24
代写ece5550: applied kalman ...
2024-12-24
代做conmgnt 7049 – measurem...
2024-12-24
代写ece3700j introduction to...
2024-12-24
代做adad9311 designing the e...
2024-12-24
代做comp5618 - applied cyber...
2024-12-24
代做ece5550: applied kalman ...
2024-12-24
代做cp1402 assignment - netw...
2024-12-24
热点标签
mktg2509
csci 2600
38170
lng302
csse3010
phas3226
77938
arch1162
engn4536/engn6536
acx5903
comp151101
phl245
cse12
comp9312
stat3016/6016
phas0038
comp2140
6qqmb312
xjco3011
rest0005
ematm0051
5qqmn219
lubs5062m
eee8155
cege0100
eap033
artd1109
mat246
etc3430
ecmm462
mis102
inft6800
ddes9903
comp6521
comp9517
comp3331/9331
comp4337
comp6008
comp9414
bu.231.790.81
man00150m
csb352h
math1041
eengm4100
isys1002
08
6057cem
mktg3504
mthm036
mtrx1701
mth3241
eeee3086
cmp-7038b
cmp-7000a
ints4010
econ2151
infs5710
fins5516
fin3309
fins5510
gsoe9340
math2007
math2036
soee5010
mark3088
infs3605
elec9714
comp2271
ma214
comp2211
infs3604
600426
sit254
acct3091
bbt405
msin0116
com107/com113
mark5826
sit120
comp9021
eco2101
eeen40700
cs253
ece3114
ecmm447
chns3000
math377
itd102
comp9444
comp(2041|9044)
econ0060
econ7230
mgt001371
ecs-323
cs6250
mgdi60012
mdia2012
comm221001
comm5000
ma1008
engl642
econ241
com333
math367
mis201
nbs-7041x
meek16104
econ2003
comm1190
mbas902
comp-1027
dpst1091
comp7315
eppd1033
m06
ee3025
msci231
bb113/bbs1063
fc709
comp3425
comp9417
econ42915
cb9101
math1102e
chme0017
fc307
mkt60104
5522usst
litr1-uc6201.200
ee1102
cosc2803
math39512
omp9727
int2067/int5051
bsb151
mgt253
fc021
babs2202
mis2002s
phya21
18-213
cege0012
mdia1002
math38032
mech5125
07
cisc102
mgx3110
cs240
11175
fin3020s
eco3420
ictten622
comp9727
cpt111
de114102d
mgm320h5s
bafi1019
math21112
efim20036
mn-3503
fins5568
110.807
bcpm000028
info6030
bma0092
bcpm0054
math20212
ce335
cs365
cenv6141
ftec5580
math2010
ec3450
comm1170
ecmt1010
csci-ua.0480-003
econ12-200
ib3960
ectb60h3f
cs247—assignment
tk3163
ics3u
ib3j80
comp20008
comp9334
eppd1063
acct2343
cct109
isys1055/3412
math350-real
math2014
eec180
stat141b
econ2101
msinm014/msing014/msing014b
fit2004
comp643
bu1002
cm2030
联系我们
- QQ: 9951568
© 2021
www.rj363.com
软件定制开发网!