DESKTOP

Algorithms for Compiler Design: IMPLEMENTATION in Bottom-up Parsing

7/24/2010 7:51:01 PM
5.3 IMPLEMENTATION
A convenient way to implement a bottom-up parser is to use a shift-reduce technique: a parser goes on shifting the input symbols onto the stack until a handle comes on the top of the stack. When a handle appears on the top of the stack, it performs reduction. This implementation makes use of a stack to hold grammar symbols and an input buffer to hold the string w to be parsed, which is terminated by the right endmarker $, the same symbol used to mark the bottom of the stack. The configuration of the parser is given by a token pair-the first component of which is a stack content, and second component is an unexpended input.

Initially, the parser will be in the configuration given by the pair ($, w$); that is, the stack is initially empty, and the buffer contains the entire string w. The parser shifts zero or more symbols from the input on to the stack until handle α appears on the top of the stack. The parser then reduces α to the left side of the appropriate production. This cycle is repeated until the parser either detects an error or until the stack contains a start symbol and the input is empty, giving the configuration ($S, $). If the parser enters ($S, $), then it announces the successful completion of parsing. Thus, the primary operation of the parser is to shift and reduce.

For example consider the bottom-up parser for the grammar having the productions:

and the input string: id+id * id. The various steps in parsing this string are shown in Table 1 in terms of the contents of the stack and unspent input.

Table 1: Steps in Parsing the String id + id * id

Stack Contents

Input

Moves

$

id + id*id$

shift id

$id

+ id*id$

reduce by F id

$F

+ id*id$

reduce by T F

$T

+ id*id$

reduce by E T

$E

+ id*id$

shift +

$E +

id*id$

shift id

$E + id

*id$

reduce by F id

$E + F

*id$

reduce by T F

$E + T

*id$

shift *

$E + T

* id$

shift id

$E + T*id

$

reduce by F id

$E + T *F

$

reduce by T T *F

$E + T

$

reduce by E E + T

$E

$

accept

Shift-reduce implementation does not tell us anything about the technique used for detecting the handles; hence, it is possible to make use of any suitable technique to detect handles. Depending upon the technique that is used to detect handles, we get different shift-reduce parsers. For example, an operator-precedence parser is a shift-reduce parser that uses the precedence relationship between certain pairs of terminals to guide the selection of handles. Whereas LR parsers make use of a deterministic finite automata that recognizes the set of all viable prefixes; by reading the stack from bottom to top, it determines what handle, if any, is on the top of the stack.


Other  
 
Top 10
Mobile Application Security : The Apple iPhone - Push Notifications, Copy/Paste, and Other IPC
Exploring the T-SQL Enhancements in SQL Server 2005 : The WAITFOR Command
Parallel Programming with Microsoft .Net : Parallel Aggregation - Variations
Optimizing an Exchange Server 2010 Environment : Analyzing Capacity and Performance
Programming .NET Security : Hashing Algorithms Explained
Sharepoint 2007: Specify Your Colleagues
Algorithms for Compiler Design: THE NFA WITH ∈-MOVES
Choosing The Right Parts For Your Build (Part 1) - Picking the perfect processor
Choosing The Right Parts For Your Build (Part 5) - Choosing your case & Picking the right storage
SQL Server 2008 : Leveraging the Microsoft Sync Framework
Most View
Legal Trouble with Social Networks (Part 1)
The choices of mobile computing for SOHO users (part 2)
Infrastructure Security: The Application Level
Sharepoint 2007: Create a New List Item
SQL Azure Data Access
Getting Started with MySQL Enterprise & MySQL Enterprise Components
iPhone Application Development : Using Advanced Interface Objects and Views - User Input and Output
How to Protect Your Mobile Devices
Joomla! Blogging and RSS Feeds : Commenting anyone?
The Second BlackBerry Developers Conference Asia (Part 2)
Windows Azure : Understanding the Blob Service
Windows Server 2008 : Understanding the Identity Management for UNIX Components
Migrating from Legacy SharePoint to SharePoint Server 2010 : Using Visual Upgrade
Designing and Implementing Mobility in Exchange Server 2010 : Securing Access to ActiveSync Using Internet Security and Acceleration (ISA) Server 2006
SQL Server 2008 : Explaining Advanced Query Techniques - Creating CTEs
Configuring Server Roles in Windows 2008 : New Roles in 2008
Mass Effect Infiltrator
iPhone 3D Programming : Anti-Aliasing Tricks with Offscreen FBOs (part 1) - A Super Simple Sample App for Supersampling
Changes in Windows Vista Affecting SDI
Search for a File or Directory