-
Notifications
You must be signed in to change notification settings - Fork 1
lasergoat/parser
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
there is G, our input will be G' (I added lambda to the terminal Σ). 1. Consider the following grammar, G = (N, S, P, S), where N = {S, L, D, X, P, R }, Σ = { n, t, nt, a, sc, |, =>, rnum, $, s, lambda } and P = { 1: S => L; 2: L => L D sc; 3: L => D sc; 4: D => n ; 5: D => X ; 6: X => t a ; 7: X => nt a; 8: X => X a ; 9: D => s ; 10: D => P ; 11: P => P | R; 12: P => rnum a => R; 13: R => R a; 14: R => lambda; } This grammar generates a language, L(G), that is a specification language for Context-free grammars! That is, if x ∈ L(G), then x is a specification for a Context-free grammar (any and all CFGs). An example of such an “x” is given below. To understand the semantics of the grammar, G, we introduce the following conventions: (a) “sc” denotes a semi-colon “;”. (b) All specifications must end with a “$”. (c) The nonterminal D derives each statement in a specification, a statement can be any one of the following: “n” (a grammar name declaration), “nt” (a declaration of the nonterminal alphabet), “t” (a declaration of the terminal alphabet), “s” (a declaration of the start symbol), “P” ( a production or re-writing rule), “rnum” ( a rule number, e.g. “12: “ ). (d) The letter “a” denotes any symbol of the vocabulary. (e) The vertical bar “|” is used as a delimiter to separate all possible rightparts for a given leftpart of a rule. # SAMPLE INPUT (input.g) # Comments begin with pound sign # Grammar must have name, non-terminal, terminal alphabet, start symbol # and a least one rule where the leftpart is the start symbol. # Every line, except with comments and the $ must end with a semicolon # Note: bos and eof are reserved symbols !name: Sample; !start: S; !non-terminals: S X Y Z; !terminals: a b; # rules are in one of the three following forms # there is only one rule per line 1: S => X|Y|Z; 2: X => b; 3: X => a; 4: Z =>; # lambda rule # The dollar sign signals the end of the grammar $ ALGORITHM (Bottom-up Parsing for WP Grammars) Input: G = (N, S, P, S) a Weak-precedence grammar. Let T be the table of precedence relations for [VG ∪{$}] × [Σ ∪{$}], where T[X,Y] ⊆ ; T[X,Y] = φ if and only if the symbols X,Y are not related by any WW relation. Let P be the table of productions ordered in descending order by length of right-part; P[i].right-part will denote the right-part of production #(i), while P[i].left-part will denote the left-part. Let Stk denote the parse stack initialized to the special marker ($); Stk[Top] will denote the top symbol of Stk; Stk should be thought of as a string with the rightmost symbol corresponding to Stk[Top]. Finally, let Action be a table of function pointers. The function identified by Action[k] is designed to perform some semantic processing action associated with rule k of the grammar, G; e.g., generate object code or enter a declaration in a Symbol table, etc. Procedure: // C will be set to "$" on an end-of-file. (1) Set C = Next_Source_Token. (2) If C = $ and Stk = "$S" Then Accept and Halt. (3) Set X = Stk[Top]. (4) Case T[X,C] { When φ ==> Report Syntax Error and Halt. // Shift input When ==> Push(C, Stk); Goto (1) When ==> for k in 1..#P { if (for some θ) Stk = θP[k].rightpart then { // Reduce Stack Set Stk = θP[k].leftpart; Action[k]; Goto (3) } } Report Syntax Error and Halt. // Note: this is the mathematical specification for finding the // the first rule in the Production table (P) such that the rightpart // matches the top of the stack (remember how P is organized). // When such a rule is found, pop the rightpart from the stack and // push the leftpart on the stack. If no rule in P matches the stack, // halt and report a “Syntax Error”. }//end case
About
takes in a grammar G' and is parsed according to G, the results are outputted
Resources
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published