diff --git a/lib/modules/LR.pmod/Grammar_parser.pmod b/lib/modules/LR.pmod/Grammar_parser.pmod index 1cc3317f7acec2cd5ce4c50deebbd9629bd6af56..0c4659e027980ac0ca245a8c582712210427e2f9 100755 --- a/lib/modules/LR.pmod/Grammar_parser.pmod +++ b/lib/modules/LR.pmod/Grammar_parser.pmod @@ -3,42 +3,34 @@ #pike __REAL_VERSION__ /* - * $Id: Grammar_parser.pmod,v 1.11 2000/12/01 19:55:47 js Exp $ + * $Id: Grammar_parser.pmod,v 1.12 2001/11/19 00:44:34 nilsson Exp $ * * Generates a parser from a textual specification. * * Henrik Grubbstr�m 1996-12-06 */ -//. -//. File: Grammar_parser.pmod -//. RCSID: $Id: Grammar_parser.pmod,v 1.11 2000/12/01 19:55:47 js Exp $ -//. Author: Henrik grubbstr�m (grubba@infovav.se) -//. -//. Synopsis: Generates an LR parser from a textual specification. -//. -//. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -//. -//. This module generates an LR parser from a grammar specified according -//. to the following grammar: -//. -//. directives : directive ; -//. directives : directives directive ; -//. directive : declaration ; -//. directive : rule ; -//. declaration : "%token" terminals ";" ; -//. rule : nonterminal ":" symbols ";" ; -//. rule : nonterminal ":" symbols action ";" ; -//. symbols : symbol ; -//. symbols : symbols symbol ; -//. terminals : terminal ; -//. terminals : terminals terminal ; -//. symbol : nonterminal ; -//. symbol : "string" ; -//. action : "{" "identifier" "}" ; -//. nonterminal : "identifier" ; -//. terminal : "string"; -//. +//! This module generates an LR parser from a grammar specified according +//! to the following grammar: +//! +//! @pre{ +//! directives : directive ; +//! directives : directives directive ; +//! directive : declaration ; +//! directive : rule ; +//! declaration : "%token" terminals ";" ; +//! rule : nonterminal ":" symbols ";" ; +//! rule : nonterminal ":" symbols action ";" ; +//! symbols : symbol ; +//! symbols : symbols symbol ; +//! terminals : terminal ; +//! terminals : terminals terminal ; +//! symbol : nonterminal ; +//! symbol : "string" ; +//! action : "{" "identifier" "}" ; +//! nonterminal : "identifier" ; +//! terminal : "string"; +//! @} /* * Includes @@ -187,8 +179,7 @@ static private object(parser) g; static private object master; -//. + error -//. Error code from the parsing. +//! Error code from the parsing. int error; static private int add_nonterminal(string id) @@ -320,14 +311,13 @@ void create() _parser->compile(); } -//. - make_parser -//. -//. Compiles the parser-specification given in the first argument. -//. Named actions are taken from the object if available, otherwise -//. left as is. -//. -//. BUGS: Returns error-code in both Grammar_parser.error and -//. return_value->error. +//! Compiles the parser-specification given in the first argument. +//! Named actions are taken from the object if available, otherwise +//! left as is. +//! +//! @bugs +//! Returns error-code in both Grammar_parser.error and +//! return_value->error. object(parser) make_parser(string str, object|void m) { object(parser) res = 0; @@ -370,20 +360,13 @@ object(parser) make_parser(string str, object|void m) return (res); } -//. - make_parser_from_file -//. -//. Compiles the file specified in the first argument into an LR parser. -//. -//. SEE ALSO: Grammar_parser.make_parser +//! Compiles the file specified in the first argument into an LR parser. +//! +//! @seealso +//! @[make_parser] int|object(parser) make_parser_from_file(string fname, object|void m) { - object(Stdio.File) f = Stdio.File(); - int|object(parser) g = 0; - if (f->open(fname, "r")) { - g = make_parser(f->read(0x7fffffff), m); - f->close(); - } - return(g); + return make_parser(Stdio.read_file(fname), m); } /* diff --git a/lib/modules/LR.pmod/item.pike b/lib/modules/LR.pmod/item.pike index 033df7f3e072d318d8cd17b085f95d258302f6a9..85c803b4420f75a2d57b2ac70875a3d499dab47a 100644 --- a/lib/modules/LR.pmod/item.pike +++ b/lib/modules/LR.pmod/item.pike @@ -1,5 +1,5 @@ /* - * $Id: item.pike,v 1.6 2000/09/28 03:38:44 hubbe Exp $ + * $Id: item.pike,v 1.7 2001/11/19 00:44:34 nilsson Exp $ * * An LR(0) item * @@ -8,57 +8,42 @@ #pike __REAL_VERSION__ -//. -//. File: item.pike -//. RCSID: $Id: item.pike,v 1.6 2000/09/28 03:38:44 hubbe Exp $ -//. Author: Henrik Grubbstr�m (grubba@infovav.se) -//. -//. Synopsis: An LR(0) item -//. -//. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -//. -//. A partially parsed rule. -//. +//! +//! An LR(0) item, a partially parsed rule. +//! import LR; /* constant kernel = (program)"kernel"; */ /* constant item = (program)"item"; */ -//. + r -//. The rule +//! The rule object(rule) r; -//. + offset -//. How long into the rule the parsing has come. +//! How long into the rule the parsing has come. int offset; -//. + next_state -//. The state we will get if we shift according to this rule +//! The state we will get if we shift according to this rule object /* (kernel) */ next_state; -//. + master_item -//. Item representing this one (used for shifts). +//! Item representing this one (used for shifts). object /* (item) */ master_item; -//. + direct_lookahead -//. Look-ahead set for this item. +//! Look-ahead set for this item. multiset(string) direct_lookahead = (<>); -//. + error_lookahead -//. Look-ahead set used for detecting conflicts + +//! Look-ahead set used for detecting conflicts multiset(string) error_lookahead = (<>); -//. + relation -//. Relation to other items (used when compiling). + +//! Relation to other items (used when compiling). multiset(object /* (item) */ ) relation = (<>); -//. + counter -//. Depth counter (used when compiling). + +//! Depth counter (used when compiling). int counter; -//. + number -//. Item identification number (used when compiling). +//! Item identification number (used when compiling). int number; -//. + item_id -//. Used to identify the item. -//. Equal to r->number + offset. +//! Used to identify the item. +//! Equal to r->number + offset. int item_id; diff --git a/lib/modules/LR.pmod/parser.pike b/lib/modules/LR.pmod/parser.pike index 4b12e2ab9a71a5035c1a468ed914a83d5a0c1a67..cf072ea87c93ac8f4488684ad106d73a39ec5b04 100644 --- a/lib/modules/LR.pmod/parser.pike +++ b/lib/modules/LR.pmod/parser.pike @@ -1,5 +1,5 @@ /* - * $Id: parser.pike,v 1.25 2000/12/01 19:55:47 js Exp $ + * $Id: parser.pike,v 1.26 2001/11/19 00:44:34 nilsson Exp $ * * A BNF-grammar in Pike. * Compiles to a LALR(1) state-machine. @@ -9,24 +9,16 @@ #pike __REAL_VERSION__ -//. -//. File: parser.pike -//. RCSID: $Id: parser.pike,v 1.25 2000/12/01 19:55:47 js Exp $ -//. Author: Henrik Grubbstr�m (grubba@infovav.se) -//. -//. Synopsis: LALR(1) parser and compiler. -//. -//. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -//. -//. This object implements an LALR(1) parser and compiler. -//. -//. Normal use of this object would be: -//. -//. {add_rule, set_priority, set_associativity}* -//. set_symbol_to_string -//. compile -//. {parse}* -//. +//! This object implements an LALR(1) parser and compiler. +//! +//! Normal use of this object would be: +//! +//! @pre{ +//! {add_rule, set_priority, set_associativity}* +//! set_symbol_to_string +//! compile +//! {parse}* +//! @} /* * Includes @@ -62,44 +54,37 @@ import LR; * Classes */ -//. o kernel -//. Implements an LR(1) state +//! Implements an LR(1) state class kernel { - //. + rules - //. Used to check if a rule already has been added when doing closures. + + //! Used to check if a rule already has been added when doing closures. multiset(object(rule)) rules = (<>); - //. + items - //. Contains the items in this state. + //! Contains the items in this state. array(object(item)) items = ({}); - //. + item_id_to_items - //. Used to lookup items given rule and offset + //! Used to lookup items given rule and offset mapping(int:object(item)) item_id_to_item = ([]); - //. + symbol_items - //. Contains the items whose next symbol is this non-terminal. + //! Contains the items whose next symbol is this non-terminal. mapping(int : multiset(object(item))) symbol_items = ([]); - //. + action - //. The action table for this state - //. - //. object(kernel) SHIFT to this state on this symbol. - //. object(rule) REDUCE according to this rule on this symbol. + //! The action table for this state + //! + //! @pre{ + //! object(kernel) SHIFT to this state on this symbol. + //! object(rule) REDUCE according to this rule on this symbol. + //! @} mapping(int|string : object(kernel)|object(rule)) action = ([]); - //. + closure_set - //. The symbols that closure has been called on. + //! The symbols that closure has been called on. multiset closure_set = (<>); /* * Functions */ - //. - add_item - //. Add an item to the state. - //. > i - //. Item to add. + //! Add an item to the state. void add_item(object(item) i) { int|string symbol; @@ -118,10 +103,10 @@ class kernel { } } - //. - closure - //. Make the closure of this state. - //. > nonterminal - //. nonterminal to make the closure on. + //! Make the closure of this state. + //! + //! @param nonterminal + //! Nonterminal to make the closure on. void closure(int nonterminal) { closure_set[nonterminal] = 1; @@ -154,8 +139,7 @@ class kernel { } } - //. - goto_set - //. Make the goto-set of this state. + //! Make the goto-set of this state. multiset(int|string) goto_set() { multiset(int|string) set = (<>); @@ -174,11 +158,11 @@ class kernel { return (set); } - //. - do_goto - //. Generates the state reached when doing goto on the specified symbol. - //. i.e. it compiles the LR(0) state. - //. > symbol - //. symbol to make goto on. + //! Generates the state reached when doing goto on the specified symbol. + //! i.e. it compiles the LR(0) state. + //! + //! @param symbol + //! Symbol to make goto on. object(kernel) do_goto(int|string symbol) { multiset(object(item)) items; @@ -245,24 +229,22 @@ class kernel { }; -//. o state_queue -//. -//. This is a queue, which keeps the elements even after they are retrieved. +//! This is a queue, which keeps the elements even after they are retrieved. class state_queue { - //. + head - //. Index of the head of the queue. + + //! Index of the head of the queue. int head; - //. + tail - //. Index of the tail of the queue. + + //! Index of the tail of the queue. int tail; - //. + arr - //. The queue itself. + + //! The queue itself. array(object(kernel)) arr = allocate(64); - //. - push - //. Pushes the state on the queue. - //. > state - //. State to push. + //! Pushes the state on the queue. + //! + //! @param state + //! State to push. object(kernel) push(object(kernel) state) { if (tail == sizeof(arr)) { @@ -273,8 +255,7 @@ class state_queue { return(state); } - //. - next - //. Return the next state from the queue. + //! Return the next state from the queue. int|object(kernel) next() { if (head == tail) { @@ -285,8 +266,7 @@ class state_queue { } } -//. + grammar -//. The grammar itself. +//! The grammar itself. mapping(int|string : array(object(rule))) grammar = ([]); /* Priority table for terminal symbols */ @@ -307,25 +287,26 @@ static private mapping(mixed : multiset(object(rule))) begins = ([]); */ static private mapping(int : multiset(object(rule))) used_by = ([]); -//. + start_state -//. The initial LR0 state. +//! The initial LR0 state. object(kernel) start_state; -//. + verbose -//. Verbosity level -//. 0 - none -//. 1 - some +//! Verbosity level +//! +//! @int +//! @value 0 +//! None +//! @value 1 +//! Some +//! @endint int verbose=1; -//. + error -//. Error code +//! Error code int error=0; /* Number of next rule (used only for conflict resolving) */ static private int next_rule_number = 1; -//. + known_states -//. LR0 states that are already known to the compiler. +//! LR0 states that are already known to the compiler. mapping(string:object(kernel)) known_states = ([]); /* @@ -347,10 +328,10 @@ static private string builtin_symbol_to_string(int|string symbol) static private function(int|string : string) symbol_to_string = builtin_symbol_to_string; -//. - rule_to_string -//. Pretty-prints a rule to a string. -//. > r -//. Rule to print. +//! Pretty-prints a rule to a string. +//! +//! @param r +//! Rule to print. string rule_to_string(object(rule) r) { string res = symbol_to_string(r->nonterminal) + ":\t"; @@ -365,10 +346,10 @@ string rule_to_string(object(rule) r) return(res); } -//. - item_to_string -//. Pretty-prints an item to a string. -//. > i -//. Item to pretty-print. +//! Pretty-prints an item to a string. +//! +//! @param i +//! Item to pretty-print. string item_to_string(object(item) i) { array(string) res = ({ symbol_to_string(i->r->nonterminal), ":\t" }); @@ -392,17 +373,16 @@ string item_to_string(object(item) i) return(res * ""); } -//. - state_to_string -//. Pretty-prints a state to a string. -//. > state -//. State to pretty-print. +//! Pretty-prints a state to a string. +//! +//! @param state +//! State to pretty-print. string state_to_string(object(kernel) state) { return (map(state->items, item_to_string) * "\n"); } -//. - cast_to_string -//. Pretty-prints the current grammar to a string. +//! Pretty-prints the current grammar to a string. string cast_to_string() { array(string) res = ({}); @@ -425,10 +405,10 @@ string cast_to_string() return (res * ""); } -//. - cast -//. Implements casting. -//. > type -//. Type to cast to. +//! Implements casting. +//! +//! @param type +//! Type to cast to. mixed cast(string type) { if (type == "string") { @@ -439,12 +419,12 @@ mixed cast(string type) /* Here come the functions that actually do some work */ -//. - set_priority -//. Sets the priority of a terminal. -//. > terminal -//. Terminal to set the priority for. -//. > pri_val -//. Priority; higher = prefer this terminal. +//! Sets the priority of a terminal. +//! +//! @param terminal +//! Terminal to set the priority for. +//! @param pri_val +//! Priority; higher = prefer this terminal. void set_priority(string terminal, int pri_val) { object(priority) pri; @@ -456,12 +436,12 @@ void set_priority(string terminal, int pri_val) } } -//. - set_associativity -//. Sets the associativity of a terminal. -//. > terminal -//. Terminal to set the associativity for. -//. > assoc -//. Associativity; negative - left, positive - right, zero - no associativity. +//! Sets the associativity of a terminal. +//! +//! @param terminal +//! Terminal to set the associativity for. +//! @param assoc +//! Associativity; negative - left, positive - right, zero - no associativity. void set_associativity(string terminal, int assoc) { object(priority) pri; @@ -473,13 +453,13 @@ void set_associativity(string terminal, int assoc) } } -//. - set_symbol_to_string -//. Sets the symbol to string conversion function. -//. The conversion function is used by the various *_to_string functions -//. to make comprehensible output. -//. > s_to_s -//. Symbol to string conversion function. -//. If zero or not specified, use the built-in function. +//! Sets the symbol to string conversion function. +//! The conversion function is used by the various *_to_string functions +//! to make comprehensible output. +//! +//! @param s_to_s +//! Symbol to string conversion function. +//! If zero or not specified, use the built-in function. void set_symbol_to_string(void|function(int|string:string) s_to_s) { if (s_to_s) { @@ -489,10 +469,10 @@ void set_symbol_to_string(void|function(int|string:string) s_to_s) } } -//. - add_rule -//. Add a rule to the grammar. -//. > r -//. Rule to add. +//! Add a rule to the grammar. +//! +//! @param r +//! Rule to add. void add_rule(object(rule) r) { array(object(rule)) rules; @@ -661,9 +641,8 @@ static private object(kernel) first_state() return(state); } -//. + s_q -//. Contains all states used. -//. In the queue-part are the states that remain to be compiled. +//! Contains all states used. +//! In the queue-part are the states that remain to be compiled. object(state_queue) s_q; static private object(ADT.Stack) item_stack; @@ -1114,8 +1093,7 @@ static private int repair(object(kernel) state, multiset(int|string) conflicts) } } -//. - compile -//. Compiles the grammar into a parser, so that parse() can be called. +//! Compiles the grammar into a parser, so that parse() can be called. int compile() { int error = 0; /* No error yet */ @@ -1435,22 +1413,25 @@ int compile() return (error); } -//. - parse -//. Parse the input according to the compiled grammar. -//. The last value reduced is returned. -//. NOTA BENE: -//. The parser must have been compiled (with compile()) -//. prior to calling this function. -//. BUGS -//. Errors should be throw()n. -//. > scanner -//. The scanner function. It returns the next symbol from the input. -//. It should either return a string (terminal) or an array with -//. a string (terminal) and a mixed (value). -//. EOF is indicated with the empty string. -//. > action_object -//. Object used to resolve those actions that have been specified as -//. strings. +//! Parse the input according to the compiled grammar. +//! The last value reduced is returned. +//! +//! @note +//! The parser must have been compiled (with compile()) +//! prior to calling this function. +//! +//! @bugs +//! Errors should be throw()n. +//! +//! @param scanner +//! The scanner function. It returns the next symbol from the input. +//! It should either return a string (terminal) or an array with +//! a string (terminal) and a mixed (value). +//! EOF is indicated with the empty string. +//! +//! @param action_object +//! Object used to resolve those actions that have been specified as +//! strings. mixed parse(object|function(void:string|array(string|mixed)) scanner, void|object action_object) { diff --git a/lib/modules/LR.pmod/priority.pike b/lib/modules/LR.pmod/priority.pike index 08260d1bc16bbbe51364cb0a00476155b95b32e1..87e258b45699c5ac4dfc69679c6a1562cc51f58a 100644 --- a/lib/modules/LR.pmod/priority.pike +++ b/lib/modules/LR.pmod/priority.pike @@ -1,5 +1,5 @@ /* - * $Id: priority.pike,v 1.4 2000/09/28 03:38:44 hubbe Exp $ + * $Id: priority.pike,v 1.5 2001/11/19 00:44:35 nilsson Exp $ * * Rule priority specification * @@ -8,36 +8,31 @@ #pike __REAL_VERSION__ -//. -//. File: priority.pike -//. RCSID: $Id: priority.pike,v 1.4 2000/09/28 03:38:44 hubbe Exp $ -//. Author: Henrik Grubbstr�m (grubba@infovav.se) -//. -//. Synopsis: Rule priority specification. -//. -//. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -//. -//. Specifies the priority and associativity of a rule. -//. +//! +//! Specifies the priority and associativity of a rule. +//! -//. + value -//. Priority value +//! Priority value int value; -//. + assoc -//. Associativity -//. -//. -1 - left -//. 0 - none -//. +1 - right +//! Associativity +//! +//! @int +//! @value -1 +//! Left +//! @value 0 +//! None +//! @value 1 +//! Right +//! @endint int assoc; -//. - create -//. Create a new priority object. -//. > p -//. Priority. -//. > a -//. Associativity. +//! Create a new priority object. +//! +//! @param p +//! Priority. +//! @param a +//! Associativity. void create(int p, int a) { value = p; diff --git a/lib/modules/LR.pmod/rule.pike b/lib/modules/LR.pmod/rule.pike index 133d94ea4e44968f3248f1ae72ab7fe032c06e1e..be2b65177fd421eb5547a40ad97d464ec1b9bcd4 100644 --- a/lib/modules/LR.pmod/rule.pike +++ b/lib/modules/LR.pmod/rule.pike @@ -1,5 +1,5 @@ /* - * $Id: rule.pike,v 1.7 2000/09/28 03:38:45 hubbe Exp $ + * $Id: rule.pike,v 1.8 2001/11/19 00:44:35 nilsson Exp $ * * A BNF-rule. * @@ -8,48 +8,35 @@ #pike __REAL_VERSION__ -//. -//. File: rule.pike -//. RCSID: $Id: rule.pike,v 1.7 2000/09/28 03:38:45 hubbe Exp $ -//. Author: Henrik Grubbstr�m (grubba@infovav.se) -//. -//. Synopsis: Implements a BNF rule. -//. -//. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -//. -//. This object is used to represent a BNF-rule in the LR parser. -//. +//! +//! This object is used to represent a BNF-rule in the LR parser. +//! /* * Object variables */ -//. + nonterminal -//. Non-terminal this rule reduces to. +//! Non-terminal this rule reduces to. int nonterminal; -//. + symbols -//. The actual rule +//! The actual rule array(string|int) symbols; -//. + action -//. Action to do when reducing this rule. -//. function - call this function. -//. string - call this function by name in the object given to the parser. -//. The function is called with arguments corresponding to the values of -//. the elements of the rule. The return value of the function will be -//. the value of this non-terminal. The default rule is to return the first -//. argument. +//! Action to do when reducing this rule. +//! function - call this function. +//! string - call this function by name in the object given to the parser. +//! The function is called with arguments corresponding to the values of +//! the elements of the rule. The return value of the function will be +//! the value of this non-terminal. The default rule is to return the first +//! argument. function|string action; /* Variables used when compiling */ -//. + has_tokens -//. This rule contains tokens +//! This rule contains tokens int has_tokens = 0; -//. + num_nonnullables -//. This rule has this many non-nullable symbols at the moment. +//! This rule has this many non-nullable symbols at the moment. int num_nonnullables = 0; /* @@ -57,45 +44,44 @@ multiset(int) prefix_nonterminals = (<>); multiset(string) prefix_tokens = (<>); */ -//. + number -//. Sequence number of this rule (used for conflict resolving) -//. Also used to identify the rule. +//! Sequence number of this rule (used for conflict resolving) +//! Also used to identify the rule. int number = 0; -//. + pri -//. Priority and associativity of this rule. +//! Priority and associativity of this rule. object /* (priority) */ pri; /* * Functions */ -//. - create -//. Create a BNF rule. -//. EXAMPLE: -//. The rule -//. -//. rule : nonterminal ":" symbols ";" { add_rule }; -//. -//. might be created as -//. -//. rule(4, ({ 9, ";", 5, ";" }), "add_rule"); -//. -//. where 4 corresponds to the nonterminal "rule", 9 to "nonterminal" -//. and 5 to "symbols", and the function "add_rule" is too be called -//. when this rule is reduced. -//. > nt -//. Non-terminal to reduce to. -//. > r -//. Symbol sequence that reduces to nt. -//. > a -//. Action to do when reducing according to this rule. -//. function - Call this function. -//. string - Call this function by name in the object given to the parser. -//. The function is called with arguments corresponding to the values of -//. the elements of the rule. The return value of the function will become -//. the value of this non-terminal. The default rule is to return the first -//. argument. +//! Create a BNF rule. +//! +//! @example +//! The rule +//! +//! rule : nonterminal ":" symbols ";" { add_rule }; +//! +//! might be created as +//! +//! rule(4, ({ 9, ";", 5, ";" }), "add_rule"); +//! +//! where 4 corresponds to the nonterminal "rule", 9 to "nonterminal" +//! and 5 to "symbols", and the function "add_rule" is too be called +//! when this rule is reduced. +//! +//! @param nt +//! Non-terminal to reduce to. +//! @param r +//! Symbol sequence that reduces to nt. +//! @param a +//! Action to do when reducing according to this rule. +//! function - Call this function. +//! string - Call this function by name in the object given to the parser. +//! The function is called with arguments corresponding to the values of +//! the elements of the rule. The return value of the function will become +//! the value of this non-terminal. The default rule is to return the first +//! argument. void create(int nt, array(string|int) r, function|string|void a) { mixed symbol;