Key Concepts:

Basic Parsing Steps:

  1. Define the Grammar: You write code that specifies the expected structure of valid input using PetitParser's building blocks. These blocks include parsers for characters, numbers, keywords, sequences, choices, repetitions, etc.
  2. Create a Parser: You combine these building blocks to create a parser object that reflects your grammar. PetitParser provides various functions and operators to assemble these blocks.
  3. Parse the Input: You feed the input string to the parser object.
  4. Get the Result: The parser returns a result indicating success or failure. In case of success, it provides the parsed data structure representing the breakdown of the input string based on the grammar.

Key Components of Grammar Rules

  1. Non-Terminal Symbols: These are like "building blocks" of your language, representing parts that need further breakdown. They are often named with capitalized words (e.g., Expression, Term, Factor).
  2. Terminal Symbols: These are the "actual characters" or "tokens" that appear in the language, like numbers, operators (+, ), or parentheses. They are typically written as literals or enclosed in quotes, such as "+", "(", "1".
  3. Production Rules: Each non-terminal symbol has rules that define how it can be "expanded" into other non-terminals and terminals. Each rule has a left-hand side (the non-terminal being defined) and a right-hand side (the sequence of symbols that define it).
  4. Choices and Sequences: