Showing posts with label string parser. Show all posts
Showing posts with label string parser. Show all posts

Tuesday, 3 February 2015

Parsing tool in erlang

Erlang Parser : Leex and Yecc

Generating The Lexer


To start off with, you need a lexer grammar to generate the lexer. A lexer grammar is essentially a set of rules defining how to tokenize an input stream based on regular expressions. 

 The lexer grammar contains three sections:
  1. Definitions - these are the atoms for each character class that can composed into match expressions
  2. Rules - these tell the lexer what series of characters to expect and what token to output when a match occurs
  3. Erlang code - this contains functions that can be used in the rule definitions
Example of Subtitle file parser using lexx and yecc

srtTokenGenerator.xrl
Definitions.

D = [0-9]

Rules.

[\s\t\r\n]+ : skip_token. 

\r : skip_token.

[\n] : skip_token.

{D}+\n : {token,{record_number,list_to_integer(removeNewLine(TokenChars,[]))}}
--> : {token ,{break,TokenLine}}.

\n(.+)\n+ : {token ,{content,removeNewLine(tl(TokenChars),[])}}.

{D}+ : {token,{hours,list_to_integer(TokenChars)}}.

:{D}+ : {token,{time,list_to_integer(tl(TokenChars))}}.

,{D}+ : {token,{milliSeconds,list_to_integer(tl(TokenChars))}}.

Erlang code.

removeNewLine([],Res) -> lists:reverse(Res);
removeNewLine([$\n|Sofar],Res) -> removeNewLine(Sofar,Res);
removeNewLine([$\r|Sofar],Res) -> removeNewLine(Sofar,Res);
removeNewLine([H|Sofar],Res) -> removeNewLine(Sofar,[H|Res]).


Explanations  

SRT is perhaps the most basic of all subtitle formats.
It consists of four parts, all in text..

1. A number indicating which subtitle it is in the sequence.
2. The time that the subtitle should appear on the screen, and then disappear.
3. The subtitle itself.
4. A blank line indicating the start of a new subtitle.


Sample SRT file

1
00:02:17,440 --> 00:02:20,375
Senator, we're making
our final approach into Coruscant.

2
00:02:20,476 --> 00:02:22,501
Very good, Lieutenant.

To create token generator erlang source code by
1> leex:file(srtTokenAnalyzer).

{ok,"./srtTokenAnalyzer.erl"}
then, it will create file srtTokenAnalyzer.erl

run srtTokenAnalyzer.erl by
2>srtTokenAnalyzer:string(" // Content of the srt file  //").

Output is  {ok,Tokens}

{ok,[{record_number,1},
     {hours,0},
     {time,0},
     {time,39},
     {milliSeconds,799},
     {break,2},
     {hours,0},
     {time,0},
     {time,42},
     {milliSeconds,39},
     {content,"When I was lying there in the VA hospital,"},
     {record_number,2},
     {hours,0},
     {time,0},
     {time,42},
     {milliSeconds,176},
     {break,6},
     {hours,0},
     {time,0},
     {time,45},
     {milliSeconds,136},
     {content,"with a big hole blown through the middle of my life,"},
     {record_number,3},
     {hours,0},
     {time,0},
     {time,...},
     {...}|...],

Generating The Parser

Now that you have an input stream that is tokenized in the way you want it, you can create your parser grammar. A grammar for yecc consists of five components:
  1. Nonterminals - these are syntactic elements that are composed of terminal systems in various patterns
  2. Terminals - these are indivisible symbols that form building blocks for composed structures. These are the tokens supplied by the lexer
  3. Rootsymbol - this is the root of the syntax tree, i.e. the contextual starting point for interpreting the input symbols
  4. Rules - these tell the parser how to classify and structure the tokens it is consuming. A rule has a name, the pattern it matches and the action to be taken when the rule fires. These return the final data structures to the application, so this is the point where you can perform any contextual transformations that are necessary
  5. Erlang code - as with the lexer grammar, this is where you can define functions to abbreviate the actions defined in the rules
Nonterminals srt record records  time_duration time_format.
Terminals record_number break content hours time milliSeconds .
Rootsymbol srt.
srt -> records : '$1'.
records -> record : '$1'.
records -> record records : ['$1'|'$2'].
record -> record_number time_duration content : ['$1','$2', '$3'].
time_duration ->
time_format break time_format : {'duration',[{'start','$1'},{'end','$3'}]}.
time_format ->
hours time time milliSeconds : {element(2,'$1'),element(2,'$2'),element(2,'$3'),element(2,'$4')}.


To create srt parser erlang source code by
1> yecc:yecc("srtParser.yrl","srtParser.erl").
{560,20}

run srtParser.erl by
2>srtParser:parse(Tokens).

Output is

{ok,[[{record_number,1},
      {duration,[{start,{0,0,39,799}},{'end',{0,0,42,39}}]},
      {content,"When I was lying there in the VA hospital,"}],
     [{record_number,2},
      {duration,[{start,{0,0,42,176}},{'end',{0,0,45,136}}]},
      {content,"with a big hole blown through the middle of my life,"}],
     [{record_number,3},
      {duration,[{start,{0,0,45,936}},{'end',{0,0,48,436}}]},
      {content,"I started having these dreams of flying."}],
     [{record_number,4},
      {duration,[{start,{0,0,49,806}},{'end',{0,0,51,266}}]},
      {content,"I was free."}],
     [{record_number,5}.....
]]}