Методические указания (1114907), страница 21
Текст из файла (страница 21)
}
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" + " " + $ID.text + " " + "D0" + constants.size());
}
else {
instructions.add("(" + $instructionNumber + ") " + "assign" + " " + $ID.text + " " + $expression.text);
}
}
else {
instructions.add("(" + $instructionNumber + ") " + "assign" + " " + $ID.text + " (" + $expression.instructionNumber + ")");
}
}
;
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 instructionNumber]
: 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";
$instructionNumber = $arifmetic_expression.instructionNumber;
}
| function_call {
$expressionType = $function_call.resultType;
$expressionClass = "function_call";
$instructionNumber = $function_call.instructionNumber;
}
| array_element {
$expressionType = "int";
$expressionClass = "array_element";
$instructionNumber = $array_element.instructionNumber;
}
| getting_address {
$expressionType = "int";
$expressionClass = "getting_address";
$instructionNumber = $getting_address.instructionNumber;
}
;
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 instructionNumber]
: operandFirst = arifmetic_operand arifmetic_sign operandSecond = arifmetic_operand {
$instructionNumber = getInstructionNumber();
instructions.add("(" + $instructionNumber + ") " + $arifmetic_sign.text + " " + $operandFirst.text + " " + $operandSecond.text);
}
;
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 instructionNumber]
: 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")) {
$instructionNumber = getInstructionNumber();
if ($expression.expressionClass.equals("const_string")) {
constants.add($expression.text);
instructions.add("(" + $instructionNumber + ") " + "[]" + " " + $ID.text + " " + "D0" + constants.size());
}
else {
instructions.add("(" + $instructionNumber + ") " + "[]" + " " + $ID.text + " " + $expression.text);
}
}
else {
$instructionNumber = getInstructionNumber();
instructions.add("(" + $instructionNumber + ") " + "[]" + " " + $ID.text + " (" + $expression.instructionNumber + ")");
}
}
}
;
getting_address returns[int instructionNumber]
: '&' ID {
if (!names.isExist($ID.text)) {
errors.add("line " + $ID.line + ": name " + $ID.text + " is not declarated");
}
else {
$instructionNumber = getInstructionNumber();
instructions.add("(" + $instructionNumber + ") " + "&" + " " + $ID.text);
}
}
;
function_call returns [String resultType, int instructionNumber]
: ID '(' parametres[$ID.line] ')' {
$instructionNumber = getInstructionNumber();
if ($ID.text.equals("strlen")) {
$resultType = "int";
instructions.add("(" + $instructionNumber + ") " + "call" + " strlen" + " 1");
}
else {
$resultType = "void";
instructions.add("(" + $instructionNumber + ") " + "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());
}
else {
instructions.add("(" + getInstructionNumber() + ") " + "param" + " " + $expressionFirst.text);
}
$parametresCount++;
}
else {
instructions.add("(" + getInstructionNumber() + ") " + "param" + " (" + $expressionFirst.instructionNumber + ")");
$parametresCount++;
}
}
}
} (',' expressionNext = expression {
if ($expressionNext.expressionType != null) {
if ($expressionNext.expressionType.equals("void")) {
errors.add("line " + $functionLine + ": wrong parameter type of " + $expressionNext.text);
}
else {
if ($expressionNext.expressionClass.equals("id") ||
$expressionNext.expressionClass.equals("const_string") ||
$expressionNext.expressionClass.equals("const_int")) {
if ($expressionNext.expressionClass.equals("const_string")) {
constants.add($expressionNext.text);
instructions.add("(" + getInstructionNumber() + ") " + "param" + " " + "D0" + constants.size());
}
else {
instructions.add("(" + getInstructionNumber() + ") " + "param" + " " + $expressionNext.text);
}
$parametresCount++;
}
else {
instructions.add("(" + getInstructionNumber() + ") " + "param" + " (" + $expressionNext.instructionNumber + ")");
$parametresCount++;
}
}
}
})*)
;
variable_declaration
: type (idParam = ID | assignParam = assign) (',' (idParamNext = ID | assignParamNext = assign))* ';' {
if (names.isExist($idParam.text)) {
errors.add("line " + $idParam.line + ": name " + $idParam.text + " duplicated");
}
else {
if ($idParam.text != null) {
names.add(names.new Name($idParam.text, $type.text, $idParam.line));
}
}
if (names.isExist($assignParam.idName)) {
errors.add("line " + $assignParam.idLine + ": name " + $assignParam.idName + " duplicated");
}
else {
if ($assignParam.idName != null) {
names.add(names.new Name($assignParam.idName, $type.text, $assignParam.idLine));
}
}
if (names.isExist($idParamNext.text)) {
errors.add("line " + $idParamNext.line + ": name " + $idParamNext.text + " duplicated");
}
else {
if ($idParamNext.text != null) {
names.add(names.new Name($idParamNext.text, $type.text, $idParamNext.line));
}
}
if (names.isExist($assignParamNext.idName)) {
errors.add("line " + $assignParamNext.idLine + ": name " + $assignParamNext.idName + " duplicated");
}
else {
if ($assignParamNext.idName != null) {
names.add(names.new Name($assignParamNext.idName, $type.text, $assignParamNext.idLine));
}
}
if ($assignParam.expressionType != null && $assignParam.idName != null && !$assignParam.expressionType.equals(names.get($assignParam.idName).getType())) {
errors.add("line " + $assignParam.idLine + ": name " + $assignParam.idName + " type is mismatched");
}