Recursive Descent Parsing Patters 1.) Production: A -> B C Parser code: getA(tokenList) { getB(tokenList); // this will consume some tokens from tokenList getC(tokenList); // this will consume more tokens from tokenList } 2.) Production: A -> B | C Parser code: getA(tokenList) { if ( PREDICT_B == tokenList.peek() ) // PREDICT_B should be a single token { getB(tokenList); // this will consume some tokens from tokenList } else { getC(tokenList); // this will consume more tokens from tokenList } } 3.) Production: A -> [ B ] C Parser code: getA(tokenList) { if ( PREDICT_B == tokenList.peek() ) // PREDICT_B should be a single token { getB(tokenList); // this will consume some tokens from tokenList } getC(tokenList); // this will consume more tokens from tokenList } 4.) Production: A -> B [ C ] There are two ways we might write the parser code: getA(tokenList) { getB(tokenList); // this will consume some tokens from tokenList if ( ! tokenList.isEmpty() ) { getC(tokenList); // this will consume more tokens from tokenList } } or getA(tokenList) { getB(tokenList); // this will consume some tokens from tokenList if ( PREDICT_C == tokenList.peek() ) { getC(tokenList); // this will consume more tokens from tokenList } } The second way is better since the first way is assuming that this production is not part of any other productions, which may not be the case. The second way does require the existence of a token PREDICT_C that distinguishes production C from all other productions. 5.) Production: A -> B 'x' C Parser code: getA(tokenList) { getB(tokenList); // this will consume some tokens from tokenList tokenList.match('x'); // consume and match one token getC(tokenList); // this will consume more tokens from tokenList } 6.) Production: A -> B ( 'x' C )* D Parser code: getA(tokenList) { getB(tokenList); // this will consume some tokens from tokenList while ( 'x' == tokenList.peek() ) // assume that D cannot begin with 'x'! { tokenList.match('x'); // consume and match the 'x' token getC(tokenList); // this will consume some tokens from tokenList } getD(tokenList); } 7.) Production: A -> B ( 'x' C )+ D Parser code: getA(tokenList) { getB(tokenList); // this will consume some tokens from tokenList do { tokenList.match('x'); // consume and match the 'x' token getC(tokenList); // this will consume some tokens from tokenList } while ( 'x' == tokenList.peek() ); getD(tokenList); } 8.) Here is an example of a more general production. A -> B ( C | 'x' D ) E* Parser code: getA(tokenList) { getB(tokenList); if ( 'x' == tokenList.peek() ) // assume that C cannot begin with 'x'! { tokenList.match('x'); getD(tokenList); } else { getC(tokenList); } while ( PREDICT_E == tokenList.peek() ) { getE(tokenList); } } 9.) Here is a slight change in the previous example. A -> B [ C | 'x' D ] E* Parser code: getA(tokenList) { getB(tokenList); if ( 'x' == tokenList.peek() ) // assume that C cannot begin with 'x'! { tokenList.match('x'); getD(tokenList); } else if ( PREDICT_C == tokenList.peek() ) { getC(tokenList); } while ( PREDICT_E == tokenList.peek() ) { getE(tokenList); } } 10.) Here is an example of a more general production. A -> B ( 'x' [C | D] )* Parser code: