From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with archive (Exim 4.43) id 1ICb7g-0006ck-4k for mharc-grub-devel@gnu.org; Sun, 22 Jul 2007 09:05:20 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1ICb7e-0006aS-Mh for grub-devel@gnu.org; Sun, 22 Jul 2007 09:05:18 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1ICb7d-0006Zr-BK for grub-devel@gnu.org; Sun, 22 Jul 2007 09:05:18 -0400 Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1ICb7d-0006Zh-47 for grub-devel@gnu.org; Sun, 22 Jul 2007 09:05:17 -0400 Received: from smtp-vbr16.xs4all.nl ([194.109.24.36]) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1ICb7c-0002LE-CU for grub-devel@gnu.org; Sun, 22 Jul 2007 09:05:16 -0400 Received: from localhost.localdomain (249-174.surfsnel.dsl.internl.net [145.99.174.249]) by smtp-vbr16.xs4all.nl (8.13.8/8.13.8) with ESMTP id l6MD5E83027708 for ; Sun, 22 Jul 2007 15:05:15 +0200 (CEST) (envelope-from mgerards@xs4all.nl) From: Marco Gerards To: The development of GRUB 2 References: <20070703175316.GA2306@ws3.vdp.com> Mail-Copies-To: mgerards@xs4all.nl Date: Sun, 22 Jul 2007 15:06:49 +0200 In-Reply-To: <20070703175316.GA2306@ws3.vdp.com> (bean123@126.com's message of "Wed, 4 Jul 2007 01:53:16 +0800") Message-ID: <87ps2kfuza.fsf@xs4all.nl> User-Agent: Gnus/5.110006 (No Gnus v0.6) Emacs/21.4 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: by XS4ALL Virus Scanner X-detected-kernel: FreeBSD 4.6-4.9 Subject: Re: Standalone problem to test syntax rules X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: The development of GRUB 2 List-Id: The development of GRUB 2 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 22 Jul 2007 13:05:18 -0000 Bean writes: Hi Bean, > 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" "\ >>dd" > CMD > TEXT > STR "aa" > TEXT > STR "aa" > VAR "BB" > STR "cc" > TEXT > STR "dd" [...] > 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: 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