logo资料库

TINY+文法规则.doc

第1页 / 共7页
第2页 / 共7页
第3页 / 共7页
第4页 / 共7页
第5页 / 共7页
第6页 / 共7页
第7页 / 共7页
资料共7页,全文预览结束
ID=letter (letter | digit)*
Part 2 Syntax of TINY+
TINY+ We define here a programming language called TINY+, which is a superset of TINY in that it includes declarations, if statement, do-while statement, string type and so on. The following consists of: 1 Lexical conventions of the language, including a description of the tokens of the language 2 EBNF description of each language construct 3 An description of the main semantics 4 Sample programs in TINY+
Part 1 Lexical Conventions of TINY+ 1. The keywords of the language are the following: true false or if then else and end not int bool string while do repeat until read write All keywords are reserved and must be written in lowcase 2. Special symbols are the following: > { <= >= , ' } ; := + - * / ( ) < = 3. Other tokens are ID, NUM and STRING which are defined by the following regular expressions: ID=letter (letter | digit)* Identifier is letter followed by letters and digits NUM=digit digit* STRING=' any character except ' ' A STRING is enclosed in brackets '…', any character except ' can appear in a STRING. A STRING can’t be defined more than a line letter=a|…|z|A|…|Z digit=0|…|9 Lower and uppercase letters are distinct 4. White space consists of blanks, newlines and tabs. White space is ignored except that it must separate IDs, NUMs, and keywords 5. Comments are enclosed in curly brackets {…} and cannot be nested.
Comments can include more than one line.
Part 2 Syntax of TINY+ An EBNF grammar for TINY+ is as follows: 1 program -> declarations stmt-sequence 2 declarations -> decl ; declarations |ε 3 decl -> type-specifier varlist 4 type-specifier -> int | bool | string 5 varlist -> identifier { , identifier } 6 stmt-sequence -> statement { ; statement } 7 statement -> if-stmt | repeat-stmt | assign-stmt | read-stmt | write-stmt | while-stmt 8 while-stmt -> while bool-exp do stmt-sequence end 9 if-stmt -> if bool-exp then stmt-sequence [else stmt-sequence] end 10 repeat-stmt -> repeat stmt-sequence until bool-exp 11 assign-stmt -> identifier:=exp 12 read-stmt -> read identifier 13 write-stmt -> write exp 14 exp -> arithmetic-exp | bool-exp | string-exp| comparison-exp 15 comparison-exp -> arithmetic-exp comparison-op arithmetic-exp 16 comparison-op -> < | = | > | >= | <= 17 arithmetic-exp -> term { addop term } 18 addop -> + | - 19 term -> factor { mulop factor }
20 mulop -> * | / 21 factor -> (arithmetic-exp) | number | identifier 22 bool-exp -> bterm { or bterm } 23 bterm -> bfactor { and bfactor} 24 bfactor -> true | false | identifier | (bool-exp) | not bfactor | (comparison-exp) 25 string-exp -> string | identifier
26 Part 3 Main semantics description of TINY+  A program consists of variable declarations and a sequence of statements. Variable declarations may be empty but there must be at least one statement.  All variables must be declared before they are used, and each variable name can be declared only once  The type of variables and expressions may be int, bool or string, type checking must be done on them
Part 4 Sample programs in TINY+ string str; int x, fact; str:= 'sample program in TINY+ language- computes factorial' ; read x; if x>0 and x<100 then {don’t compute if x<=0} fact:=1; while x>0 do fact:=fact*x; x:=x-1 end; write fact end
分享到:
收藏