330 likes | 413 Views
A Tool for Constructing Syntax-Directed Editors. Yung-Shen Chang and Nai-Wei Lin Department of Computer Science and Information Engineering National Chung-Cheng University Taiwan, R.O.C. Outline. Introduction A generator for incremental parsers A simple interface to incremental parsers
E N D
A Tool for Constructing Syntax-Directed Editors Yung-Shen Chang and Nai-Wei Lin Department of Computer Science and Information Engineering National Chung-Cheng University Taiwan, R.O.C.
Outline • Introduction • A generator for incremental parsers • A simple interface to incremental parsers • Conclusions and future work
… Edit Compile Found Bugs Edit Compile Found Bugs Typical Program Development Cycle • Program editing and program compilation are separated. • It is more efficient if we can do program editing and program compilation at the same time.
Syntax-Directed Editors • Syntax-directed editors have the ability to perform program editing and program compilation at the same time. • Syntax-directed editors have become an integral feature for modern integrated development environment.
Incremental Parsing • The main capability of syntax-directed editors is incremental parsing. • Incremental parsing has the ability to only parse the modified portion of a program. • This ability allows us to interleave program editing and program compilation.
Contributions • A generator for incremental parsers that is based on Bison. • A simple interface to incremental parsers to facilitate the integration of editors and incremental parsers to form syntax-directed editors.
Grammar definition (sample.y) Parser (sample.tab.c) Bison Bison
Outline • Introduction • A generator for incremental parsers • A simple interface to incremental parsers • Conclusions
Incremental Parsing Algorithms • The state matching incremental parsing algorithm (Larchêveque) • no need to change Bison parsing table • The sentential-form incremental parsing algorithm (Wagner) • need to change Bison parsing table
Threaded Tree Parsing • Use threaded tree data structure to represent the parse tree • Threaded tree is a combination of parse tree and parse stack • All shift/reduce actions are operated on the threaded tree
Threaded Parse Tree • Each node in the threaded tree has the following fields • symbol • children • state • threading
Shift Action • Shift symbol M at state S • construct a node N (M, S) • set N’s threading to TOS • set TOS (Top of Stack) as N TOS N1 N (M, S)
Reduce Action • Reduce symbol M at State S with k children • construct a node N (M, S) • collect k nodes from TOS • construct connections between N and its children • set N’s threading to leftmost node of its children • set TOS as N M → sr N(M,S) N1 s1 r2 TOS
An Example Input= fghf Action: Shift f2 Shift g6 Reduce G4 Shift h9 Reduce H7 Reduce F3 Shift f6 Reduce S1 S1 TOS f2 F3 f6 BOS G4 H7 g6 h9
Incremental Threaded Tree Parsing • Split previous parse tree into three parts x y z • x and z are unmodified parts • y is the modified part • Let y’ be the modifying content x y z
Incremental Threaded Tree Parsing • Finding initial configuration (x) • Parse exhaustively (y’) • Finding optimal grafting point from candidates (z) • Perform node grafting
Finding Candidates • A grafting point must be a common ancestor of y’ and y • We start searching from nearest common ancestor (NCA)
Proper Grafting Points • A candidate for a grafting point is an NCA n such that • terminal successor of n = current lookahead • symbol of n = symbol of the TOS • predecessor of n = predecessor of the TOS
= deleted node An Example S1 Grafting Point BOS0 F3 f6 f2 F3 G4 x: g5 y: g8 z: h9, f6 H7 G4 g8 TOS NCA g5 h9 lookahead
incparser parser 4 Perform grafting if find suitable grafting point Incremental Parser Modules 3 1 Perform action (shift/reduce) input read manage tree node node 2 node Determine what to do … Parsing table reader read = module Bison Parsing Table = file
Outline • Introduction • A generator for incremental parsers • A simple interface to incremental parsers • Conclusions
Interface to Incremental Parser • Editor calls incparser to get service from incremental parser • Editor passes parameters begin, end, and delta • incparser will store the parsing result in variable err void incparser (POS begin, POS end, conststring& delta, ERR& err);
Flow of an Incremental Parsing Session 6 5 Return parse result Return parse result interface incparser editor 1 Call interface 3 Wrapper call incparser 2 Write delta Temporary file 4 Read input from temporary file
Proper Timing to Trigger Incremental Parsing • By timer • By each key press • By editing model
Editing Model • Editing is a sequence of key presses • Classify key presses into two groups • modikeys–which will cause content change • non-modikeys–which won’t cause content change • Editing causes a state change between pressing modikeys and non-modikeys • Editor should remember BEGIN and END position when a state change occurs
Special Keys • BS (Backspace)/DEL are keys in modikeysthat need special treatments • BS might change theBEGIN position • DEL might change the END position • Editor should perform appropriate action when user pressing BS/DEL • We use a counter to maintain how many modikeys are pressed (except BS/DEL)
An Example • Assume that the cursor is at the position between “ma”, “in”, the editor is atnon-modikey state • User performs the following modifications • press BS two times • press DEL two times • key in “foo_function” • key in any non-modikeys
01 int main (void) { 02 printf(“Hello, World\n”); 03 return 0; 04 } 01 int in (void) { 02 printf(“Hello, World\n”); 03 return 0; 04 } An Example (cont.) • Initial situation • cursor at (1, 7) • BEGIN and END is N/A • Pressing BS two times • cursor at (1, 5) • BEGIN = (1, 5) • END = (1, 8)
01 int in (void) { 02 printf(“Hello, World\n”); 03 return 0; 04 } 01 int (void) { 02 printf(“Hello, World\n”); 03 return 0; 04 } An Example (cont.) • Press DEL two times • cursor at (1, 5) • BEGIN = (1, 5) • END = (1, 10)
01 int (void) { 02 printf(“Hello, World\n”); 03 return 0; 04 } 01 int foo_function (void) { 02 printf(“Hello, World\n”); 03 return 0; 04 } An Example (cont.) • Key in “foo_function” • cursor at (1, 17) • BEGIN = (1, 5) • END = (1, 10) • DELTA = “foo_function” • Press any non-modikeys Call incparser by • BEGIN = (1, 5) • END = (1, 10) • DELTA = “foo_function”
Outline • Introduction • A generator for incremental parsers • A simple interface to incremental parsers • Conclusions
Conclusions • We developed a generator for incremental parsers based on Bison • We introduced a simpleinterfaceto incremental parsers that facilitate integration of an incremental parser and an editor based on editing model
Future Work • A generator for incremental lexers • A generator for incremental semantics analyzers • A generator for syntax-directed editors