I am helping my nephew for my C lab homework, this is a string manipulation assignment and Wang's algorithm Applies.
Here is the BNF representation for the input.
& lt; S & gt; :: = & lt; L & gt; # & Lt; R> & Lt; L & gt; :: = & lt; List & gt; | Ε & lt; R> :: = & lt; List & gt; | Ε & lt; List & gt; :: = & lt; F & gt; | & Lt; F & gt; , & Lt; List & gt; & Lt; F & gt; :: = & lt; Letter & gt; | - & lt; F & gt; | (<> F & gt; & lt; f & gt;) & lt; Op & gt; :: = & amp; | | | & Gt; & Lt; Letter & gt; :: = A | ... | Z
What is the best practice to handle and parse such input in C? How can I parse this structure without using struct
? thank you in advanced.
The simplest approach is to make each rule (or "production") a function. This is called the "recurring descent" parser.
Write two routers which will show a glimpse and also get the next character.
This will give you some code that looks like this (in pseudocode):
// < Successive & gt; :: = & lt; Lhs & gt; # & Lt; Rhs & gt; Sequence () LHS () if pecker ()! = '#' Error Other PoundsSwine = Aggression () rhs () // & lt; Lhs & gt; :: = & lt; Formulalist & gt; | Ε LHS () If Peker () == EOF return formula () // & lt; Rhs & gt; :: = & lt; Formulalist & gt; | Ε rhs () if picar () == eof return to other formulist () // & lt; Formulalist & gt; :: = & lt; Formula & gt; | & Lt; Formula & gt; , & Lt; Formulalist & gt; Formulist () formulas () If Pekhchchar () is ',' Comma = Next Charya () Return formula () // & lt; Formula & gt; :: = & lt; Letter & gt; | - & lt; Formula & gt; | (& Lt; formula & gt; & lt; in fi xop & gt; & lt; formula & gt;) formula () next = peekchar () If the next is in the next letter in the letter - minus_sign = nextchar () Return formula () and formula () infixop ()) formulas () // & lt; In fi xop> :: = & amp; | | | & Gt; Infixop () c = nextchar () if c no & amp;, |, & gt; Error // Even more, for each rule
General Thoughts:
P> - Each rule is a function
- At some points the function looks forward to what to do. For example, the formula () checks whether the first letter is zero symbol.
Comments
Post a Comment