SimplePascal Grammar
Lyt
一、简介
1.
数据类型:整型integer、浮点型real、布尔型boolean、指针、pointer(相当于C++中void*)
2.
有且只有一个main函数,从调用main函数开始执行程序
3.
大小写不敏感
二、词法
1.
关键字
词法单元
|
记号
|
说明
|
function
|
keywordFUNCTION
|
函数(有返回值)
|
procedure
|
keywordPROCEDURE
|
函数(无返回值)
|
var
|
keywordVAR
|
声明变量
|
result
|
keywordRESULT
|
函数的返回值
|
integer
|
keywordINTEGER
|
整型
|
real
|
keywordREAL
|
浮点型
|
boolean
|
keywordBOOLEAN
|
布尔型
|
pointer
|
keywordPOINTER
|
相当于C++中void*
|
true
|
keywordTRUE
|
真
|
false
|
keywordFALSE
|
假
|
or
|
keywordOR
|
或
|
and
|
keywordAND
|
与
|
not
|
keywordNOT
|
非
|
while
|
keywordWHILE
|
|
do
|
keywordDO
|
|
repeat
|
keywordREPEAT
|
|
until
|
keywordUNTIL
|
|
break
|
keywordBREAK
|
|
continue
|
keywordCONTINUE
|
|
if
|
keywordIF
|
|
then
|
keywordTHEN
|
|
else
|
keywordELSE
|
|
begin
|
keywordBEGIN
|
模块开始
|
end
|
keywordEND
|
模块结束
|
sizeof
|
keywordSIZE_OF
|
获得某类型字节
|
2.
标识符:字母或下划线开头,由字母、下划线、数字构成
词法单元
|
记号
|
说明
|
标识符
|
ID
|
[_a-zA-Z]+([_0-9a-zA-Z])*
|
3.
字面量
词法单元
|
记号
|
说明
|
整数
|
valueINTEGER
|
[0-9]+
|
浮点数
|
valueREAL
|
[0-9]+.[0-9]+
|
布尔值
|
keywordFALSE;keywordTRUE
|
false;true
|
4.
操作符
词法单元
|
记号
|
正则表达式
|
逗号
|
operatorCOMMA
|
,
|
分号
|
operatorSEMICOLON
|
;
|
冒号
|
operatorCOLON
|
:
|
赋值号
|
operatorASSIGN
|
:=
|
指针
|
operatorPOINTER
|
^
|
取址
|
operatorADDRESS
|
@
|
加
|
operatorADD
|
+
|
减
|
operatorSUB
|
-
|
乘
|
operatorMUL
|
*
|
除
|
operatorDIV
|
/
|
左括号
|
operatorLEFT
|
(
|
右括号
|
operatorRIGHT
|
)
|
等于
|
operatorEQUAL
|
=
|
不等于
|
operatorNOT_EQUAL
|
<>
|
小于
|
operatorLESS_THAN
|
<
|
大于
|
operatorLARGER_THAN
|
>
|
小于等于
|
operatorLESS_EQUAL_THAN
|
<=
|
大于等于
|
operatorLARGER_EQUAL_THAN
|
>=
|
5.
注释:不能嵌套
正则表达式:{([^}])*}
三、语法
1.
类型
DataType → PrimitiveType | PointerType
| FunctionType | VariableType
(1)
基本类型
PrimitiveType → keywordINTEGER |
keywordREAL | keywordPBOOLEAN
(2)
指针类型
PointerType → keywordPOINTER |
DataType
operatorPOINTER
如:integer^
(3)
函数类型
ParameterDeclaration → ID operatorCOLON DataType
ParameterDeclarationList →
(ParameterDeclaration (operatorSEMICOLON ParameterDeclaration)*) | EMPTY
a)
有返回值
FunctionType
→
keywordFUNCTION Expression
operatorLEFT ParameterDeclarationList
operatorRIGHT
operatorCOLON
DataType
如:function GetValue(i:integer):integer
b)
无返回值
FunctionType
→
keywordPROCEDURE Expression
operatorLEFT ParameterDeclarationList
operatorRIGHT
如:procedure Run(i:integer; p:integer^)
2.
表达式
Expression →
PrimitiveExpression
| InvokeExpression | CastExpression | SizeOfExpression |
ResultExpression
| UnaryExpression | BinaryExpression
(1)
基本表达式
PrimitiveExpression
→ LiteralExpresssion |
VariableExpression
a)
字面值表达式
A.
整数:LiteralExpresssion → valueINTEGER
如:123
B.
浮点数:LiteralExpresssion
→ valueREAL
如:12.3
C.
布尔值:LiteralExpresssion
→ keywordFALSE | keywordTRUE
如:true
b)
变量表达式:VariableExpression → ID
如:a
(2)
函数调用表达式
InvokeExpression → Expression operatorLEFT
ArgumentList operatorRIGHT
ArgumentList → EMTPY | (Expression (operatorCOMMA Expression)*)
如:FunctionName(argument1,
argument2)
(3)
强制转换表达式
CastExpression → DataType operatorLEFT Expression operatorRIGHT
如:data指针平移一个整数距离integer^(integer(data)+sizeof(integer))
注:指针无法加减,只能强制转换成整数
(4)
sizeof表达式
SizeOfExpression →
keywordSIZE_OF
operatorLEFT (Expression | DataType) operatorRIGHT
如:sizeof(integer)
(5)
result表达式
ResultExpression
→ keywordRESULT
(6)
一元表达式
a)
not:UnaryExpression → keywordNOT
Expression
如:not true
b)
取址:UnaryExpression → operatorADDRESS
Expression
如:@pointer
c)
取指针指向的值:UnaryExpression → Expression operatorPOINTER
如:pointer^
(7)
二元表达式
BinaryExpression → OrExpression
OrExpression → AndExpression (keywordOR AndExpression)*
AndExpression → RelationExpression (keywordAND RelationExpression )*
RelationExpression →
AddSubExpression ((operatorLESS |
operatorLARGER | operatorLESS_THAN |
operator LARGER_THAN | operatorEQUAL
| operator NOT_EQUAL) Expression)*
AddSubExpression →
MulDivExpression ((operatorADD | operatorSUB)
AddSubExpression)*
MulDivExpression →
UnaryExpression ((operatorMUL | operatorDIV)
UnaryExpression)*
SimpleExpression →
PrimitiveExpression | InvokeExpression | UnaryExpression |
operatorLEFT
Expression operatorRIGHT
如:IsAvailable() and (1+2)/3 = 1
(8)
操作符优先级(由低到高):括号可以改变优先级
1
|
or
|
2
|
and
|
3
|
< > <=
>= = <>
|
4
|
+ -
|
5
|
* /
|
6
|
not -(一元)
|
7
|
@
|
3.
语句
Statement →
ExpressionStetement
| AssignStatement | IfStatement |
WhileStatement
| RepeatStatement | ControlStatement | BlockStatement
(1)
表达式语句
ExpressionStatement → Expression operatorSEMICOLON
如:Run();
(2)
赋值语句
AssignStatement → Expression operatorASSIGN Expression operatorSEMICOLON
如: pointer^ = 1;
(3)
if语句
IfStatement →
keywordIF operatorLEFT Expression
operatorRIGHT
keywordTHEN Statement
(keywordELSE Statement)?
如:if (true) then DoSomething(); else
DoSomethingElse();
(4)
while语句
WhileStatement →
keywordWHILE
operatorLEFT Expression operatorRIGHT keywordDO
Statement
如:while (IsAvailable()) do DoSomething();
(5)
repeat语句
RepeatStatement →
keywordREPEAT Statement
keywordUNTIL operatorLEFT Expression
operatorRIGHT operatorSEMICOLON
如:repeat DoSomething();
until (IsAvailable());
(6)
控制语句
ControlStatement →(keywordBREAK | keywordCONTINUE) operatorSEMICOLON
(7)
语句块
BlockStatement →
keywordBEGIN (Statement)*
keywordEND operatorSEMICOLON
如:begin DoSomething();
DoSomethingElse(); end;
a)
由begin和end包围,可包围0个或以上语句
b)
可以嵌套
c)
在其所在层次上被视为一个语句
4.
函数定义
函数定义不能嵌套,只能在全局区域
FunctionProcedure → FunctionHead (VariableDeclaration | EMPTY) BlockStatement
(1)
函数头
ParameterDeclaration → ID operatorCOLON DataType
ParameterDeclarationList →
(ParameterDeclaration (operatorSEMICOLON ParameterDeclaration)*) |
EMPTY
a)
有返回值
FunctionHead →
keywordFUNCTION Expression
operatorLEFT ParameterDeclarationList
operatorRIGHT
operatorCOLON
DataType
如:function GetValue(i:integer):integer
b)
无返回值
FunctionHead →
keywordPROCEDURE Expression
operatorLEFT ParameterDeclarationList
operatorRIGHT
如:procedure Run(i:integer; p:integer^)
(2)
变量声明:只能在函数中
VariableDeclaration → keywordVAR (VariableDeclarationItem)+
VariableDeclarationItem →
VariableList operatorCOLON DataType operatorSEMICOLON
VariableList → ID (operatorCOMMA ID)*
如:var i,j:integer;
p:boolean;
函数定义如:
(1)
function
Fun1(Param1:integer;Param2:integer):boolean
var i,j:integer; k:real;
begin
{函数体}
end
(2)
procedure
Fun2()
begin
{函数体}
end