5. 请给出pl/0语言的词法规则,包括以下几种表达方式:文法、正规式、NFA、DFA。
时间: 2023-06-03 18:07:42 浏览: 159
编译原理课设 正则表达式,正规式,NFA,DFA等
以下是pl/0语言的词法规则:
文法表示:
Program → block '.'
block → [constDecl][varDecl][procedureDecl] statement
constDecl → 'const' constName '=' number {',' constName '=' number} ';'
varDecl → 'var' varName {',' varName} ';'
procedureDecl → {'procedure' procName ';' block ';'}
statement → [assignmentStatement | ifStatement | whileStatement | readStatement | writeStatement | callStatement | compoundStatement | empty]
assignmentStatement → varName := expression
ifStatement → 'if' condition 'then' statement ['else' statement]
whileStatement → 'while' condition 'do' statement
readStatement → 'read' '(' varName {',' varName} ')'
writeStatement → 'write' '(' expression {',' expression} ')'
callStatement → 'call' procName
compoundStatement → 'begin' statement {';' statement} 'end'
empty →
expression → ['+' | '-'] term {('+' | '-') term}
term → factor {('*' | '/') factor}
factor → varName | number | '(' expression ')'
condition → 'odd' expression | expression relOp expression
relOp → '=' | '<>' | '<' | '<=' | '>' | '>='
正规式表示:
数字:[0-9]+
关键字:const | var | procedure | begin | end | if | then | else | while | do | read | write | call | odd
标识符:[a-zA-Z]+
运算符:+ | - | * | / | = | <> | < | <= | > | >= | ( | )
分隔符:,| ; | .
NFA表示:
DFA表示:
阅读全文