Методические указания (1114907), страница 19
Текст из файла (страница 19)
instructions.add(resultString);
check();
}
else {
resultString += "(" + $expressionSecond.instructionNumber + ")";
instructions.add(resultString);
check();
}
}
}
}
}
}
;
logical_link
: '==' | '!=' | '>' | '<' | '>=' | '<='
;
operator returns [int instructionNumber]
: variable_declaration {
$instructionNumber = $variable_declaration.instructionNumber;
}
| assign_operator {
$instructionNumber = $assign_operator.instructionNumber;
}
| for_operator {
$instructionNumber = $for_operator.instructionNumber;
}
| function_call_operator {
$instructionNumber = $function_call_operator.instructionNumber;
}
;
program
: (operator {
if ($operator.instructionNumber != 0) {
program += "(" + $operator.instructionNumber + ") ";
}
})+
;
-
Для четверок.
grammar Task5Grammar;
options {
language = Java;
}
@members {
protected NamesTable names = new NamesTable();
protected ArrayList<String> errors = new ArrayList<String>();
protected ArrayList<String> instructions = new ArrayList<String>();
protected ArrayList<String> constants = new ArrayList<String>();
protected int currentTempNumber = 0;
protected int instructionNumber = 0;
public static void main(String[] args) throws Exception {
Task5GrammarLexer lex = new Task5GrammarLexer(new ANTLRFileStream("test.txt"));
Task5GrammarParser parser = new Task5GrammarParser(new CommonTokenStream(lex));
parser.program();
if (! parser.errors.isEmpty()) {
System.out.println("Found " + parser.errors.size() + " errors:");
for (String m : parser.errors) {
System.out.println(m);
}
}
else {
System.out.println("Compiled successfully");
System.out.println("Constants:");
int i = 1;
for (String m : parser.constants) {
System.out.println("D0" + i + " " + m);
i++;
}
System.out.println("Instructions:");
parser.instructions.add("(" + (parser.instructions.size()) + ")");
for (String m : parser.instructions) {
System.out.println(m);
}
}
}
public String getErrorHeader(RecognitionException e) {
return "line "+e.line+":";
}
public void emitErrorMessage(String msg) {
errors.add(msg);
}
public int genNewTemp() {
currentTempNumber++;
return currentTempNumber;
}
public int getInstructionNumber() {
instructionNumber++;
return instructionNumber - 1;
}
}
ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
;
DIGIT : '0'..'9'+
;
STRING : '"' ~'"'* '"'
;
WS : ( ' '
| '\t'
| '\r'
| '\n'
) {$channel=HIDDEN;}
;
type
: 'int' | 'char[]'
;
assign returns [String idName, int idLine, String expressionType, int instructionNumber]
: ID '=' (expression) {
$idName = $ID.text;
$idLine = $ID.line;
$expressionType = $expression.expressionType;
$instructionNumber = getInstructionNumber();
if ($expression.expressionClass.equals("id") ||
$expression.expressionClass.equals("const_string") ||
$expression.expressionClass.equals("const_int")) {
if ($expression.expressionClass.equals("const_string")) {
constants.add($expression.text);
instructions.add("(" + $instructionNumber + ") " + "assign" + " " + "D0" + constants.size() + " - " + $ID.text);
}
else {
instructions.add("(" + $instructionNumber + ") " + "assign" + " " + $expression.text + " - " + $ID.text);
}
}
else {
instructions.add("(" + $instructionNumber + ") " + "assign" + " t" + $expression.tempIndex + " - " + $ID.text);
}
}
;
assign_operator
: assign ';' {
if (!names.isExist($assign.idName)) {
errors.add("line " + $assign.idLine + ": name " + $assign.idName + " is not declarated");
}
else {
if (!$assign.expressionType.equals(names.get($assign.idName).getType())) {
errors.add("line " + $assign.idLine + ": name " + $assign.idName + " type is mismatched");
}
}
}
;
expression returns [String expressionType, String expressionClass, int tempIndex]
: ID {
if (!names.isExist($ID.text)) {
errors.add("line " + $ID.line + ": name " + $ID.text + " is not declarated");
}
else {
$expressionType = names.get($ID.text).getType();
$expressionClass = "id";
}
}
| STRING {
$expressionType = "char[]";
$expressionClass = "const_string";
}
| DIGIT {
$expressionType = "int";
$expressionClass = "const_int";
}
| arifmetic_expression {
$expressionType = "int";
$expressionClass = "arifmetic_expression";
$tempIndex = $arifmetic_expression.tempVariableIndex;
}
| function_call {
$expressionType = $function_call.resultType;
$expressionClass = "function_call";
$tempIndex = $function_call.tempVariableIndex;
}
| array_element {
$expressionType = "int";
$expressionClass = "array_element";
$tempIndex = $array_element.tempVariableIndex;
}
| getting_address {
$expressionType = "int";
$expressionClass = "getting_address";
$tempIndex = $getting_address.tempVariableIndex;
}
;
arifmetic_operand
: ID {
if (!names.isExist($ID.text)) {
errors.add("line " + $ID.line + ": name " + $ID.text + " is not declarated");
}
else {
if (!names.get($ID.text).getType().equals("int")) {
errors.add("line " + $ID.line + ": name " + $ID.text + " type mismatched");
}
}
}
| DIGIT
;
arifmetic_expression returns[int tempVariableIndex]
: operandFirst = arifmetic_operand arifmetic_sign operandSecond = arifmetic_operand {
$tempVariableIndex = genNewTemp();
instructions.add("(" + getInstructionNumber() + ") " + $arifmetic_sign.text + " " + $operandFirst.text + " " + $operandSecond.text + " t" + $tempVariableIndex);
}
;
arifmetic_sign
: ('+' | '-' | '*' | '/')
;
incrementation returns [String idName]
: ID '++' {
if (!names.isExist($ID.text)) {
errors.add("line " + $ID.line + ": name " + $ID.text + " is not declarated");
}
else {
if (!names.get($ID.text).getType().equals("int")) {
errors.add("line " + $ID.line + ": name " + $ID.text + " type mismatched");
}
else {
$idName = $ID.text;
}
}
}
;
array_element returns [int tempVariableIndex]
: ID '[' expression ']' {
if (!names.isExist($ID.text)) {
errors.add("line " + $ID.line + ": name " + $ID.text + " is not declarated");
}
else {
if (!names.get($ID.text).getType().equals("char[]")) {
errors.add("line " + $ID.line + ": name " + $ID.text + " type mismatched");
}
}
if (!$expression.expressionType.equals("int")) {
errors.add("line " + $ID.line + ": wrong index type");
}
else {
if ($expression.expressionClass.equals("id") ||
$expression.expressionClass.equals("const_string") ||
$expression.expressionClass.equals("const_int")) {
$tempVariableIndex = genNewTemp();
if ($expression.expressionClass.equals("const_string")) {
constants.add($expression.text);
instructions.add("(" + getInstructionNumber() + ") " + "[]" + " " + $ID.text + " " + "D0" + constants.size() + " - " + "t" + $tempVariableIndex);
}
else {
instructions.add("(" + getInstructionNumber() + ") " + "[]" + " " + $ID.text + " " + $expression.text + " - " + "t" + $tempVariableIndex);
}
}
else {
$tempVariableIndex = genNewTemp();
instructions.add("(" + getInstructionNumber() + ") " + "[]" + " " + $ID.text + " t" + $expression.tempIndex + " - " + "t" + $tempVariableIndex);
}
}
}
;
getting_address returns[int tempVariableIndex]
: '&' ID {
if (!names.isExist($ID.text)) {
errors.add("line " + $ID.line + ": name " + $ID.text + " is not declarated");
}
else {
$tempVariableIndex = genNewTemp();
instructions.add("(" + getInstructionNumber() + ") " + "&" + " " + $ID.text + " - " + " t" + $tempVariableIndex);
}
}
;
function_call returns [String resultType, int tempVariableIndex]
: ID '(' parametres[$ID.line] ')' {
if ($ID.text.equals("strlen")) {
$resultType = "int";
$tempVariableIndex = genNewTemp();
instructions.add("(" + getInstructionNumber() + ") " + "call" + " strlen" + " 1" + " t" + $tempVariableIndex);
}
else {
$resultType = "void";
instructions.add("(" + getInstructionNumber() + ") " + "call" + " " + $ID.text + " " + $parametres.parametresCount);
}
}
;
parametres [int functionLine] returns [int parametresCount]
: {
$parametresCount = 0;
}
((expressionFirst = expression) {
if ($expressionFirst.expressionType != null) {
if ($expressionFirst.expressionType.equals("void")) {
errors.add("line " + $functionLine + ": wrong parameter type of " + $expressionFirst.text);
}
else {
if ($expressionFirst.expressionClass.equals("id") ||
$expressionFirst.expressionClass.equals("const_string") ||
$expressionFirst.expressionClass.equals("const_int")) {
if ($expressionFirst.expressionClass.equals("const_string")) {
constants.add($expressionFirst.text);
instructions.add("(" + getInstructionNumber() + ") " + "param" + " " + "D0" + constants.size());