Lexical grammar
In computer science, a lexical grammar or lexical structure 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:
[0-9]+
Decimal integer literal (no leading zero):
[1-9][0-9]*|0
Hexadecimal integer literal:
0[Xx][0-9A-Fa-f]+
Octal integer literal:
0[0-7]+
Identifier:
[A-Za-z_$][A-Za-z0-9_$]*
See also
References
- ^ Buyya (2009). Object-oriented Programming with Java: Essentials and Applications. Tata McGraw-Hill Education. pp. 57–. ISBN 978-0-07-066908-6.
- ^ James Gosling (2000). The Java Language Specification. Addison-Wesley Professional. pp. 9–. ISBN 978-0-201-31008-5.
External links
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.