请输入您要查询的百科知识:

 

词条 Lexical grammar
释义

  1. Examples

  2. See also

  3. References

  4. External links

In computer science, a lexical grammar is a formal grammar defining the syntax of tokens. The program is written using characters that are defined by the lexical structure of the language used. The character set is equivalent to the alphabet used by any written language. The lexical grammar lays down the rules governing how a character sequence is divided up into subsequences of characters, each part of which represents an individual token. This is frequently defined in terms of regular expressions.[1]

For instance, the lexical grammar for many programming languages specifies that a string literal starts with a " character and continues until a matching " is found (escaping makes this more complicated), that an identifier is an alphanumeric sequence (letters and digits, usually also allowing underscores, and disallowing initial digits), and that an integer literal is a sequence of digits. So in the following character sequence "abc" xyz1 23 the tokens are string, identifier and number (plus whitespace tokens) because the space character terminates the sequence of characters forming the identifier. Further, certain sequences are categorized as keywords – these generally have the same form as identifiers (usually alphabetical words), but are categorized separately; formally they have a different token type.[2]

Examples

Regular expressions for common lexical rules follow (for example, C).

Unescaped string literal (quote, followed by non-quotes, ending in a quote):

Escaped string literal (quote, followed by escaped characters or non-quotes, ending in a quote):

Integer literal:

Decimal integer literal (no leading zero):

Hexadecimal integer literal:

Octal integer literal:

Identifier:

See also

  • Lexical analysis

References

1. ^{{cite book|author=Buyya|title=Object-oriented Programming with Java: Essentials and Applications|url=https://books.google.com/books?id=rXGMFYXFDwMC&pg=PA57|year=2009|publisher=Tata McGraw-Hill Education|isbn=978-0-07-066908-6|pages=57–}}
2. ^{{cite book|author=James Gosling|title=The Java Language Specification|url=https://books.google.com/books?id=Ww1B9O_yVGsC&pg=PA9|year=2000|publisher=Addison-Wesley Professional|isbn=978-0-201-31008-5|pages=9–}}
{{refbegin}}{{refend}}

External links

  • ANSI C grammar, Lex specification
{{DEFAULTSORT:Lexical Grammar}}

2 : Formal languages|Parsing

随便看

 

开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。

 

Copyright © 2023 OENC.NET All Rights Reserved
京ICP备2021023879号 更新时间:2024/9/23 11:29:35