编译原理翻译.pptxVIP

  • 1
  • 0
  • 约4.32千字
  • 约 3页
  • 2021-01-22 发布于广东
  • 举报
The general format of Lex source is: Lex 源的一般格式: {definitions} {定义} %% %% {rules} {规则} %% %% {user subroutines} {用户子程序} where the definitions and the user subroutines are often omitted. The second %% is optional, but the first is required to mark the beginning of the rules. The absolute minimum Lex program is thus 定义和用户子程序部分是通常省略。第二个%%是可选的,但首先是需要记录规则的开头。绝 对最小的 Lex 程序是这样如此 %% %% (no definitions, no rules) which translates into a program which copies the input to the output unchanged. (没有定义,没有规则)转译成一个程序中把输入完全没有变化地复制到输出。 In the outline of Lex programs shown above, the rules represent the user’s control decisions; they are a table, in which the left column contains regular expressions (see section 3) and the right column contains actions, program fragments to be executed when the expressions are recognized. Thus an individual rule might appear integer printf(found keyword INT); 在上面显示的 Lex 程序里,规则代表用户控制的决定;在表中,左列包含正则表达式(见第 三段)和右边栏包含动作,当表达式被组织时程序片段将执行。因此独立的规则可能出现 Integer printf(“发现的关键词 INT”); to look for the string integer in the input stream and print the message “found keyword INT” whenever it appears. In this example the host procedural language is C and the C library function printf is used to print the string. The end of the expression is indicated by the first blank or tab character. If the action is merely a single C expression, it can just be given on the right side of the line; if it is compound, or takes more than a line, it should be enclosed in braces. As a slightly more useful example, suppose it is desired to change a number of words from British to American spelling. Lex rules such as 为了寻找输入流中的字符串整数和输出信息“发现的关键词 int”而不管它何时出现。在这 个例子中,本地程序语言是 C 和 C 库函数函数 printf 是用来输出字符串。表达式的最后部分 是用第一个空白处或制表符指出的。如果这种动作只不过是一个单一的 C 表达方式,它可能 只出现在右侧;如果它是复合的,或着超过几行,它都必须加上注释。在一个稍微有用的例子, 假设这是准备把英式拼法改成美式拼法。Lex 规则如下: colour……………………输出(“color”) ;mechanise……………………输出(“mechanize”) petrol……………………输出(“gas”) would be a start. These rules are not quite enough

文档评论(0)

1亿VIP精品文档

相关文档