What is Parsing?

How it works

Small Case Study

class Date {  int day;
int month;
int year;
}

Implementation Note

Code

String date = "20-05-2012";// 1. defining a regular expression
Pattern dateMatcher = Pattern.compile("(\\d{2})-(\\d{2})-(\\d{4})");
// 2. matching a regular expression
Matcher matcher = dateMatcher.matcher(date);
// 3. if pattern matches
if (matcher.find()) {
// extract day
int day = Integer.parseInt(matcher.group(1));
int month = Integer.parseInt(matcher.group(2));
int year = Integer.parseInt(matcher.group(3));
// 4. validate date : skipped code return new Date(day, month, year);} else { // throw Error
throw new DateParseException(date +" is not valid")
}
(\d{2})-(\d{2})-(\d{4})# where \d matches any digit between 0 to 9, 
# {n} means how many times the last character type is expected
# () is called capturing group and enclosed part of string that would be extracted.
# Each group is numbered, the first group is assigned "1", the second one is given number "2", and so on

Phases of Parsing

Scanning

Syntactic Analysis

Closing Notes

These equations are built using the tool from http://www.imatheq.com

Footnotes

Software Design Enthusiast. https://themightyprogrammer.dev