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.

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.

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

Tags:

Lexical Grammar ExamplesLexical GrammarComputer scienceFormal grammarRegular expressionSyntax (programming languages)Token (parser)

🔥 Trending searches on Wiki English:

MasterChef India (Hindi season 7)Mariah Carey2023 NCAA Division I men's basketball tournamentEmily BluntLeonardo DiCaprio2023 Miami OpenRaghav ChadhaMorgan WallenMelanie LynskeyAudie MurphyPolandNATO1923 (TV series)Sam HydeMighty Morphin Power RangersIndonesiaSophie's Choice (film)Ted KaczynskiJason SudeikisRam CharanThe Pirate BayLos AngelesHolly HolmRic FlairThailandSandy Hook Elementary School shootingKelly ReillyRupert MurdochNigeriaCamila MorroneFast & FuriousAshley Johnson (actress)EminemDave Stewart (musician and producer)Rob SchneiderFranceBrazilMegan FoxChinaChiedozie OgbeneRuby RidgeNicola SturgeonNope (film)Detachment (film)TikTokChuck NorrisResident Evil 4Jennifer Lopez2023 in filmMarylandThe Last of Us Part IIChabeloBette DavisJim CarreyBrett GoldsteinHTTP 404Narendra ModiMinecraftEd SheeranQueen VictoriaUEFA Euro 2024Kellie-Jay Keen-MinshullUEFA Champions LeagueManchester United F.C.Scarlett JohanssonStormy DanielsList of NCAA Division I men's basketball tournament Final Four participantsRyan PhillippeCharles BarkleyEmail clientAnas SarwarAna de ArmasKings LeagueXXX (film series)2022–23 CONCACAF Nations League🡆 More