编译原理Tiny语言的定义
2013-07-17 10:54
531 查看
Here is the definition for Tiny language
Single-character separators:
Single-character operators:
Multi-character operators:
Identifier: An identifier consists of a letter followed by any number of letters or digits. The following are examples of identifiers: x, x2, xx2, x2x, End, END2.Note that End is an identifier while END is a keyword. The following are not identifiers:
Strings in comments are not identifiers.
Number is a sequence of digits, or a sequence of digits followed by a dot, and followed by digits.
Comments: string between /** and **/. Comments can be longer than one line.
转自:http://cs.uwindsor.ca/~jlu/214/language.htm
The Tiny lexicon is as follows:
Keywords:IF ELSE WRITE READ RETURN BEGIN END MAIN INT REAL
Single-character separators:
; ,
(
)
Single-character operators:
+ -
*
/
Multi-character operators:
:= == !=
Identifier: An identifier consists of a letter followed by any number of letters or digits. The following are examples of identifiers: x, x2, xx2, x2x, End, END2.Note that End is an identifier while END is a keyword. The following are not identifiers:
IF, WRITE, READ, .... (keywords are not counted as identifiers)
2x(identifier can not start with a digit)
Strings in comments are not identifiers.
Number is a sequence of digits, or a sequence of digits followed by a dot, and followed by digits.
Number -> Digits | Digits '.' Digits Digits -> Digit | Digit Digits Digit -> '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
Comments: string between /** and **/. Comments can be longer than one line.
The EBNF Grammar
High-level program structures
Program -> MethodDecl MethodDecl* MethodDecl -> Type [MAIN] Id '(' FormalParams ')' Block FormalParams -> [FormalParam ( ',' FormalParam )* ] FormalParam -> Type Id Type -> INT | REAL
Statements
Block -> BEGIN Statement* END Statement -> Block | LocalVarDecl | AssignStmt | ReturnStmt | IfStmt | WriteStmt | ReadStmt LocalVarDecl -> INT Id ';' | REAL Id ';' AssignStmt -> Id := Expression ';' ReturnStmt -> RETURN Expression ';' IfStmt -> IF '(' BoolExpression ')' Statement | IF '(' BoolExpression ')' Statement ELSE Statement WriteStmt -> WRITE '(' Expression ',' QString ')' ';' ReadStmt -> READ '(' Id ',' QString ')' ';' QString is any sequence of characters except double quote itself, enclosed in double quotes.
Expressions
Expression -> MultiplicativeExpr (( '+' | '-' ) MultiplicativeExpr)* MultiplicativeExpr -> PrimaryExpr (( '*' | '/' ) PrimaryExpr)* PrimaryExpr -> Num // Integer or Real numbers | Id | '(' Expression ')' | Id '(' ActualParams ')' BoolExpression -> Expression '==' Expression |Expression '!=' Expression ActualParams -> [Expression ( ',' Expression)*]
Sample program
/** this is a comment line in the sample program **/ INT f2(INT x, INT y ) BEGIN INT z; z := x*x - y*y; RETURN z; END INT MAIN f1() BEGIN INT x; READ(x, "A41.input"); INT y; READ(y, "A42.input"); INT z; z := f2(x,y) + f2(y,x); WRITE (z, "A4.output"); END
转自:http://cs.uwindsor.ca/~jlu/214/language.htm
相关文章推荐
- (编译原理)TINY扩充语言的语法分析
- 编译原理学习笔记(二)——高级语言及其语法描述
- 学习:程序设计语言——编译原理(一、引论)
- 【编译原理】【语言基础】
- 编译原理与汇编语言的概念区别
- [CS@Nets][编译原理]一个栈式计算机的加法语言编译器
- 编译原理-高级语言及其语法描述作业总结
- 编译原理:文法和语言
- 编译原理与脚本语言
- [编译原理]学习笔记(二)——文法和语言
- 编译原理Tiny语言的定义
- c语言编译原理及IPC通信经典面试题
- 编译原理高级语言及其语法描述
- 【编译原理】上下文有关语言-0型语言、1型语言、2型语言、3型语言(校招笔试)
- 【编译原理】TINY编译器学习(一)
- 编译原理中四种文法语言的基本概念
- 【编译原理】TINY编译器之词法分析(二)
- 语言,编译原理
- 编译原理---文法和语言
- 编译原理笔记4 文法与语言