Standalone problem to test syntax rules
Hi,
I have written a small problem to test my parse.y, to compile, use the following commands:
bison -d -p grub_script_yy -b grub_script parser.y gcc -oparser parser.c lexer.c grub_script.tab.c
After compilation, run parser
./parser
Just enter the commands after '##', and the syntax tree associtaed with it will be displayed.
The program support option - , which is used to disable prompt '##' and '>>'. This is useful when inputting from files:
./parser - < input_file
Some example:
## aa "aa${BB}cc" "\
Quoted 1 line
dd"
CMD
TEXT
STR "aa"
TEXT
STR "aa"
VAR "BB"
STR "cc" TEXT
STR "dd"
## if aa; then
Quoted 2 lines
bb dd$cc
fi
IF
CMDS
CMD
TEXT
STR "aa"
CMDS
CMD
TEXT
STR "bb"
TEXT
STR "dd"
VAR "cc"
## function foo {
Quoted 3 lines
set AA=1
if aa; then bb; else cc; fi
}
FUNC
TEXT
STR "foo"
CMDS
CMD
TEXT
STR "set"
TEXT
STR "AA=1"
IF
CMDS
CMD
TEXT
STR "aa"
CMDS
CMD
TEXT
STR "bb"
CMDS
CMD
TEXT
STR "cc"The reason why I like to use binary tree is:
1. Standard data type for all script elements, only one set of function is needed to manipulate the structure.
2. Enumeration and deallocation is simpler. Instead of using switch, we can enumerate the tree using its two branch, child and next.
I also figure out how to release memory when syntax error occurs. When a node is first created using grub_script_newnode, it's added to a linked list state->free_list. When it's referenced, it's moved from the linked list to the branch of a tree. If the whole script is parsed, all nodes will be moved to the final tree, but if it fails at some point, partial built element can be found in the free list. This way, we can also keep track of nodes allocated. For example, consider this command:
## aa bb
commands: command
{
$$=$1;
}
| commands command
{
$$=grub_script_catnode(state, $1, $2);
}Using the second rule, node bb is moved out of free list and move to the next branch of aa. Now the free list look like this:
root => 0
free_list => aa => 0
|
bbscript: commands '\n'
{
state->root=grub_script_getnode(state, $1);
}Now aa is moved out of free list:
root => aa => 0
|
bbfree_list => 0
In any given time, all nodes can be found in either root or free_list.
The latest version of parser support error recovery, for example:
## fi ; aa ; bb ; fi ; ee
syntax error
invalid command
syntax error
invalid command
== TREE BEGIN ==
CMD
TEXT
STR "aa"
CMD
TEXT
STR "bb"
CMD
TEXT
STR "ee"
== TREE END ==
Signature · 1 line
Bean
I forget to include the attachment. Here is it.
Signature · 1 line
Bean
Bean <[email protected]> writes:
Hi Bean,
Quoted 31 lines
I have written a small problem to test my parse.y, to compile, use the following commands:
bison -d -p
grub_script_yy-bgrub_scriptparser.y gcc -oparserparser.clexer.cgrub_script.tab.cAfter compilation, run parser
./parser
Just enter the commands after '##', and the syntax tree associtaed with it will be displayed.
The program support option - , which is used to disable prompt '##' and '>>'. This is useful when inputting from files:
./parser - <
input_fileSome example:
## aa "aa${BB}cc" "\
Quoted 1 line
dd"
CMD
TEXT
STR "aa"
TEXTSTR "aa" VAR "BB" STR "cc"TEXT
STR "dd"
[...]
Quoted 8 lines
The reason why I like to use binary tree is:
1. Standard data type for all script elements, only one set of function is needed to manipulate the structure.
2. Enumeration and deallocation is simpler. Instead of using switch, we can enumerate the tree using its two branch, child and next.
Quoted 7 lines
I also figure out how to release memory when syntax error occurs. When a node is first created using
grub_script_newnode, it's added to a linked liststate->free_list. When it's referenced, it's moved from the linked list to the branch of a tree. If the whole script is parsed, all nodes will be moved to the final tree, but if it fails at some point, partial built element can be found in the free list. This way, we can also keep track of nodes allocated. For example, consider this command:
You are right that it has advantages. But I prefer using an Abstract Syntax Tree. It is used a lot in most literature on parsers, clean and easy to understand.
The disadvantage is that you need a separate free routine for each kind of node. But heck, we can even generalize this!
I agree there is a lot of room for improvement. But stepping away from using ASTs is not the way to go in my opinion.
--
Marco
Quoted 9 lines · Marco Gerards
You are right that it has advantages. But I prefer using an Abstract Syntax Tree. It is used a lot in most literature on parsers, clean and easy to understand.
The disadvantage is that you need a separate free routine for each kind of node. But heck, we can even generalize this!
I agree there is a lot of room for improvement. But stepping away from using ASTs is not the way to go in my opinion.
Using AST or binary tree is only a matter of choice. If you think AST is better, I will modify the code to use AST.
Would you take a look at the program, especially the error recover rules. I think it can handle all situation, but you never know until it's fully tested.
Signature · 1 line
Bean
Quoted 5 lines · Bean
Quoted 4 lines · Marco Gerards
You are right that it has advantages. But I prefer using an Abstract Syntax Tree. It is used a lot in most literature on parsers, clean and easy to understand.
Quoted 8 lines
Quoted 5 lines
The disadvantage is that you need a separate free routine for each kind of node. But heck, we can even generalize this!
I agree there is a lot of room for improvement. But stepping away from using ASTs is not the way to go in my opinion.
Using AST or binary tree is only a matter of choice. If you think AST is better, I will modify the code to use AST.
The code already uses an AST. Or what do you mean?
If I am not mistaken, you are going over parser.y to make it work with more situations and to introduce proper error handling. Or am I mistaken?
Quoted 3 lines
Would you take a look at the program, especially the error recover rules. I think it can handle all situation, but you never know until it's fully tested.
In the tarball?
--
Marco
Quoted 1 line · Marco Gerards
The code already uses an AST. Or what do you mean?
I mean the test program.
Quoted 3 lines
If I am not mistaken, you are going over parser.y to make it work with more situations and to introduce proper error handling. Or am I mistaken?
There is a lot of change. The basic idea is that commands should be seperated by either ';' or '\n', followed by zero or more '\n'.
delimiter: '\n'
| ';' | delimiter '\n'
Quoted 6 lines
Quoted 4 lines
Would you take a look at the program, especially the error recover rules. I think it can handle all situation, but you never know until it's fully tested.
In the tarball?
Yes.
Signature · 1 line
Bean
Quoted 3 lines · Bean
Quoted 1 line
In the tarball?
Yes.
Could you send the files to the list as attachments? Or whatever is relevant. tarballs on a mailinglist are very inconvenient. :-/
--
Marco
Quoted 15 lines · Marco Gerards
Quoted 3 lines · Bean
Quoted 1 line
In the tarball?
Yes.
Could you send the files to the list as attachments? Or whatever is relevant. tarballs on a mailinglist are very inconvenient. :-/
--
Marco
Quoted 5 lines
_______________________________________________
Grub-devel mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/grub-devel
Hello!
I agree! Those things on a list are driving people that way on the LinuxBIOS list. While I won't name names, I can tell that the responsible party is leaning towards conventional patches.
There is as it happens a number of places where a blob can be stored for access by us, should you not be able to host them locally. Of course I can't recall them, but certainly someone on this list can do that.
Signature · 3 lines
Gregg C Levine [email protected] "This signature was once found posting rude messages in English in the Moscow subway."