Details of Steps in Parsing
The three figures that follow are to help you understand the concept of parsing. Please note that the figures do not include error cases.
The figures include terms whose definitions are as follows:
- string start
- is the beginning of the source string (or substring).
- string end
- is the end of the source string (or substring).
- length
- is the length of the source string.
- match start
- is in the source string and is the first character of the match.
- match end
- is in the source string. For a string pattern, it is the first character after the end of the match. For a positional pattern, it is the same as match start.
- match position
- is in the source string. For a string pattern, it is the first matching character. For a positional pattern, it is the position of the matching character.
- token
- is a distinct syntactic element in a template, such as a variable, a period, a pattern, or a comma.
- value
- is the numeric value of a positional pattern. This can be either a constant or the resolved value of a variable.
Figure 1. Conceptual
Overview of Parsing
┌────────────────────────────────────────┐
↓ │
┌────────────────────────────────┐ │
│START │ │
│Token is first one in template. │ │
│Length=length(source string) │ │
│Match start=1. Match end=1. │ │
└─────────┬──────────────────────┘ │
┌──────────→ │ │
│ ↓ │
│ ┌───────────────────┐yes ┌────────────────────┐ │
│ │End of template? ├───→│Parsing complete. │ │
│ └─────────┬─────────┘ └────────────────────┘ │
│ ↓ no │
│ ┌───────────────────┐ │
│ │CALL Find Next │ │
│ │ Pattern. │ │
│ └─────────┬─────────┘ │
│ ↓ │
│ ┌───────────────────┐ │
│ │CALL Word Parsing. │ │
│ └─────────┬─────────┘ │
│ ↓ │
│ ┌───────────────────┐ │
│ │Step to next token.│ │
│ └─────────┬─────────┘ │
│ ↓ │
│ ┌───────────────────┐ yes ┌────────────────────┐ │
│ │Token a comma? ├────→│Set next source │ │
│ └─────────┬─────────┘ │string and template.├───┘
│ │ no └────────────────────┘
└────────────┘
Figure 2. Conceptual View of Finding Next Pattern
┌────────────────────────────────────────────────┐
↓ │
┌─────────────┐ ┌────────────────────────────────┐ │
│Start: │yes │String start=match end. │ │
│End of ├───→│Match start=length + 1. │ │
│template? │ │Match end=length + 1. Return. │ │
└─────┬───────┘ └────────────────────────────────┘ │
↓ no │
┌─────────────┐ ┌────────────────────────────────┐ │
│Token period │yes │ │ │
│or variable? ├───→│Step to next token. ├───┘
└─────┬───────┘ └────────────────────────────────┘
↓ no
┌─────────────┐ ┌─────────┐ ┌──────────┐ ┌───────────────────────────────────┐
│Token a plus?│yes │Variable │yes │Resolve │ │String start=match start. │
│ ├───→│form? ├───→│its value.├──→│Match start=min(length + 1, │
└─────┬───────┘ └────┬────┘ └──────────┘↑ │ match start + value). │
│ no │ no │ │Match end=match start. Return. │
↓ └─────────────────────┘ └───────────────────────────────────┘
┌─────────────┐ ┌─────────┐ ┌──────────┐ ┌───────────────────────────────────┐
│Token a │yes │Variable │yes │Resolve │ │String start=match start. │
│minus? ├───→│form? ├───→│its value.├──→│Match start=max(1, match │
└─────┬───────┘ └────┬────┘ └──────────┘↑ │ start ─ value). │
│ no │ no │ │Match end=match start. Return. │
↓ └─────────────────────┘ └───────────────────────────────────┘
┌─────────────┐ ┌─────────┐ ┌──────────┐ ┌───────────────────────────────────┐
│Token an │yes │Variable │yes │Resolve │ │String start=match end. │
│equal? ├───→│form? ├───→│its value.├──→│Match start=min(length+1, value). │
└─────┬───────┘ └────┬────┘ └──────────┘↑ │Match end=match start. Return. │
│ no │ no │ └───────────────────────────────────┘
↓ └─────────────────────┘
┌─────────────┐ ┌───────────────────────────────────┐
│Token a │yes │String start=match end. │
│number? ├───→│Match start=min(length+1, value). │
└─────┬───────┘ │Match end=match start. Return. │
↓ no └───────────────────────────────────┘
┌─────────────┐
│Token a lit─ │yes
│eral string? ├──────────────────────────┐
└─────┬───────┘ │
│ no │
↓ ↓
┌─────────────┐ ┌──────────┐ ┌───────────────┐ ┌─────────────────────────────┐
│Token a var─ │yes │Resolve │ │Match found in │yes │String start=match end. │
│iable string?├───→│its value.├──→│rest of string?├───→│Match start=match position. │
└─────┬───────┘ └──────────┘ └──────┬────────┘ │Match end=match position + │
│ no │ no │ pattern length. Return. │
│ ↓ └─────────────────────────────┘
│ ┌────────────────────────────────┐
│ │String start=match end. │
│ │Match start=length + 1. │
│ │Match end=length + 1. Return. │
↓ └────────────────────────────────┘
┌─────────────┐ ┌────────────────────────────────┐
│Token a │yes │Match start=length + 1. │
│ comma? ├─────────→│Match end=length + 1. Return. │
└─────────────┘ └────────────────────────────────┘
Figure 3. Conceptual
View of Word Parsing
┌─────────────────────────┐ ┌────────────────────────┐
│Start: Match end <= │no │ │
│ string start? ├───→│String end=match start. │
└───────────┬─────────────┘ └────────────────────────┘
↓ yes
┌─────────────────────────┐
│String end=length + 1. │
└───────────┬─────────────┘
↓
┌──────────────────────────────────────────────────────────────────────┐
│Substring=substr(source string,string start,(string end─string start))│
│Token=previous pattern. │
└───────────┬──────────────────────────────────────────────────────────┘
↓ ←───────────────────────────────────────────────┐
┌─────────────────────────┐no │
│Any more tokens? ├─────────────┐ │
└───────────┬─────────────┘ │ │
↓ yes │ │
┌─────────────────────────┐ │ │
│Step to next token. │ │ │
└───────────┬─────────────┘ │ │
↓ ↓ │
┌─────────────────────────┐no ┌────────────────────────┐ │
│Token a variable or a ├───→│Return. │ │
│period? │ └────────────────────────┘ │
└───────────┬─────────────┘ │
↓ yes │
┌─────────────────────────┐no │
│Any more tokens? ├─────────────┐ │
└───────────┬─────────────┘ │ │
↓ yes ↓ │
┌─────────────────────────┐ ┌────────────────────────┐ │
│Next token a variable or │ no │Assign rest of substring│ │
│period? ├───→│to variable. │ │
└───────────┬─────────────┘ └─────────────┬──────────┘ │
↓ yes └───────────────→│
┌─────────────────────────┐ no ┌────────────────────────┐ │
│Any substring left? ├───→│Assign null string to │ │
└───────────┬─────────────┘ │variable. │ │
↓ yes └─────────────┬──────────┘ │
┌─────────────────────────┐ └───────────────→│
│Strip any leading blanks.│ │
└───────────┬─────────────┘ │
↓ │
┌─────────────────────────┐ no ┌────────────────────────┐ │
│Any substring left? ├───→│Assign null string to │ │
└───────────┬─────────────┘ │variable. │ │
│ └─────────────┬──────────┘ │
↓ yes └───────────────→│
┌─────────────────────────┐ no ┌────────────────────────┐ │
│Blank found in substring?├───→│Assign rest of substring│ │
│ │ │to variable. │ │
└───────────┬─────────────┘ └─────────────┬──────────┘ │
↓ yes └───────────────→│
┌───────────────────────────────────────────────────────────┐ │
│Assign word from substring to variable and step past blank.│ │
└───────────────────┬───────────────────────────────────────┘ │
└─────────────────────────────────────────┘