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 2 Typical Program Development Cycle … Edit Compile Found Bugs Edit Compile Found Bugs Program editing and program compilation are separated. It is more efficient if we can do program editing and program compilation at the same time. 3 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. 4 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. 5 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 syntaxdirected editors. 6 Bison Grammar definition (sample.y) Bison Parser (sample.tab.c) 7 Outline Introduction A generator for incremental parsers A simple interface to incremental parsers Conclusions 8 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 9 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 10 Threaded Parse Tree Each node in the threaded tree has the following fields – symbol – children – state – threading 11 Shift Action Shift symbol M at state S 1. construct a node N (M, S) 2. set N’s threading to TOS 3. set TOS (Top of Stack) as N TOS N1 N (M, S) 12 Reduce Action Reduce symbol M at State S with k children 1. construct a node N (M, S) 2. collect k nodes from TOS 3. construct connections between N and its children 4. set N’s threading to leftmost node of its children 5. set TOS as N M → sr N(M,S) N1 s1 r2 TOS 13 An Example Input= fghf S1 Action: Shift f2 Shift g6 TOS BOS f2 F3 f6 Reduce G4 Shift h9 G4 H7 g6 h9 Reduce H7 Reduce F3 Shift f6 Reduce S1 14 Incremental Threaded Tree Parsing Split previous parse tree into three parts xyz x and z are unmodified parts y is the modified part Let y’ be the modifying content x y z x, z = unmodified y = modified 15 Incremental Threaded Tree Parsing 1. 2. 3. 4. Finding initial configuration (x) Parse exhaustively (y’) Finding optimal grafting point from candidates (z) Perform node grafting 16 Finding Candidates A grafting point must be a common ancestor of y’ and y We start searching from nearest common ancestor (NCA) 17 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 18 An Example Grafting Point S1 BOS0 F3 f6 f2 F3 G4 G4 TOS g5 x: g5 g8 H7 y: g8 z: h9, f6 = deleted node NCA h9 lookahead 19 Incremental Parser Modules 1 input 3 read Perform action (shift/reduce) manage tree node node 2 Determine what to do incparser node … parser Parsing table reader 4 read Bison Parsing Table = module = file Perform grafting if find suitable grafting point 20 Outline Introduction A generator for incremental parsers A simple interface to incremental parsers Conclusions 21 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, const string& delta, ERR& err); 22 Flow of an Incremental Parsing Session 6 5 Return parse result interface editor 1 2 Call interface Return parse result incparser 3 Wrapper call incparser Write delta Temporary file 4 Read input from temporary file 23 Proper Timing to Trigger Incremental Parsing By timer By each key press By editing model 24 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 25 Special Keys BS (Backspace)/DEL are keys in modikeys that need special treatments BS might change the BEGIN 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) 26 An Example Assume that the cursor is at the position between “ma”, “in”, the editor is at nonmodikey state User performs the following modifications – press BS two times – press DEL two times – key in “foo_function” – key in any non-modikeys 27 An Example (cont.) Initial situation – cursor at (1, 7) – BEGIN and END is N/A 01 int main (void) { 02 printf(“Hello, World\n”); 03 return 0; 04 } 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 } 28 An Example (cont.) Press DEL two times – cursor at (1, 5) – BEGIN = (1, 5) – END = (1, 10) 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 } 29 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” 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 } 30 Outline Introduction A generator for incremental parsers A simple interface to incremental parsers Conclusions 31 Conclusions We developed a generator for incremental parsers based on Bison We introduced a simple interface to incremental parsers that facilitate integration of an incremental parser and an editor based on editing model 32 Future Work A generator for incremental lexers A generator for incremental semantics analyzers A generator for syntax-directed editors 33