Skip to content

Functions & Methods

Mohamed MERZAK edited this page Jun 25, 2020 · 8 revisions

Deterministic Finite State Automaton

public static DFSA CreateInstance(string filename) 
  • Creates a new Deterministic Finite State Automaton and returns it.
    • filename full path to the automaton's definition file.

private DFSA(string filename) 
  • Deterministic Finite State Automaton private constructor.
    • filename full path to the automaton's definition file.

public void Print() 

Displays all the Automaton's info to the STDOUT.

  • Calls ToString method in a Console.WriteLine statement.

public bool Analyse(string sourceCode) 

Analyses a source code while displaying its Lexemes (Lexical Units) & Tokens one-by-one and each whether it is accepted or not.

  • Returns bool indicating whether a sourcecode : String is accepted by the automaton's defined language.
    • sourceCode the source code the be analysed.
  • Calls Accepts & SplitToken methods.

private int Accepts(string word)

Examines a Lexeme's symbols transitions and returns the final state occurred, 0 if none (lexeme not accepted by the automaton's described language).

  • Returns int final state of the given word, 0 if none.
    • word lexeme or lexical unit.

private bool Accept(string word)

Examines a Lexeme's symbols transitions and returns true if the Lexical Unit has a valid endState, otherwise returns false.

  • Returns bool indicating whether the given word is accepted by the automaton's described language or not.
    • word lexeme or lexical unit.

public IEnumerable<string> SplitToken(string word)

Splits a Lexical Unit or Lexeme to Lexemes if they contain OPERATORS. (call in Analyse if (tokenId == 0 && word.Contains(_operators))).

  • Returns IEnumerable<string> that can be looped thru.
    • word lexeme or lexical unit.

public override string ToString() 

Returns all the Automaton's info as String.

  • Returns string representation of Automaton.



Transition

public Transition(int startState, char symbol, int endState) 

Transition public constructor.


public override string ToString() 

Returns string representation of Transition.


public bool Equals(Transition other) 

Returns bool indicating whether this & other Transitions are equal.


public int σ(int? startState, char? symbol) 

The Transition function.

  • Returns int nextstate, -1 if no next state was found.

public bool isΣ(int? startState, char? symbol) 

Returns bool indicating whether the current Transition is the one the being looked for. (call in TransitionsMap [] operator)




Transitions Map : List<Transition>

public int this[int i, char c] 

The Transition function, returns the next or final state from the given i state and c symbol, -1 if no match is found.

  • Returns int nextstate, -1 if no next state was found.

public override string ToString() 

Returns string representation of TransitionsMap (All its Transitions).




Token

public Token(int tokenId, string value) 

Token public constructor.

  • Checks for Tokens that could not be described in TokenTypes Enum.
  • Checks if the token has an accepted final state != 0.

public bool Accepted()

Returns bool indicating whether the Token is accepted or not, based on the calculation in the constructor.


public override string ToString() 

Returns string representation of Token.




Tokens List : List<Token>

public bool Accepted()

Returns bool indicating whether all the Tokens in the TokensList are accepted or not.


public override string ToString() 

Returns string representation of TokensList (All its Tokens).




Extentions

Transitions Map

public static void AddIfNotExists(this TransitionsMap transitionsMap, Transition transition) 

Adds a new Transition to the TransitionsMap if it doesn't already exist.


public static bool Has(this TransitionsMap transitionsMap, Transition transition) 

Returns bool indicating whether a given Transition exists in the TransitionsMap or not.



Token Type : enum

public static TokenType GetToken(this TokenType tokenType, int tokenId) 

Returns TokenType from a given tokenId : int.



List

public static void AddIfNotExists<T>(this List<T> list, T item) 

Adds a new item : T to the List if it doesn't already exist.



String

public static bool Contains(this string @string, IEnumerable<string> predicates) 

Returns bool indicating whether a string contains any of the predicates.


public static IEnumerable<string> Split(this string word, IEnumerable<string> separators)

Returns IEnumerable<string> containing subwords found by splitting the given word using the given separators.