Results 1 to 4 of 4

Thread: [RESOLVED] [RegEx] Syntax Checker

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,754

    Resolved [RESOLVED] [RegEx] Syntax Checker

    For those of you who do not know, I'm trying to create my own programming language. No so much to make the next best language, but because I'm really interested in doing it. What I'm trying to do is create a syntax checker for the declaration process. Here are a little bit of details on it:
    Code:
    <access modifier> <identifier> as <data type>
    Access Modifiers:
    1. local
    2. global


    Data Types:
    1. Boolean
    2. Decimal
    3. Number
    4. String


    So an example would be:
    Code:
    local fooVar as string
    Look familiar? ;]

    Anyways. Before I was simply using a bunch of conditional statements to check if the syntax was in the correct layout. Now I've decided to use RegEx. This is the pattern that I've tried:
    Code:
    \blocal|\bglobal [a-zA-Z] \bas \bboolean|\bdecimal|\bnumber|\bstring
    but whenever I try my example above it only matches local and string. If I change local to global then it doesn't match. The data type matches all types except for the Boolean type. How can I fix this?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  2. #2

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,754

    Re: [RegEx] Syntax Checker

    I was able to work it up:
    Code:
    ^(global|local) ([^\s]+) (as) (boolean|decimal|number|string)$
    Explanation:
    ^(global|local) Match either the word global or the word local at the beginning of the string
    ([^\s]+) Match the word before the whitespace
    (as) Match the word as
    (boolean|decimal|number|string)$ Match the word: Boolean, decimal, number, or string at the end of the sentence
    Last edited by dday9; Mar 25th, 2014 at 03:44 PM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,754

    Re: [RESOLVED] [RegEx] Syntax Checker

    I was able to improve it a little bit:
    Code:
    ^(global|local) ([a-zA-Z-0-9_]+) (as) (boolean|decimal|number|string)$
    Explanation:
    ^(global|local) Match either the word global or the word local at the beginning of the string
    ([a-zA-Z-0-9_]+) Match any sequence of characters that are letters, numeric, or an underscore
    (as) Match the word as
    (boolean|decimal|number|string)$ Match the word: Boolean, decimal, number, or string at the end of the sentence
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  4. #4

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,754

    Re: [RESOLVED] [RegEx] Syntax Checker

    I was able to improve it even more with the help of some people on DIC forums:
    Code:
    ^(global|local) ([a-zA-Z_][a-zA-Z-0-9_]*) (as) (boolean|decimal|number|string)$
    Explanation:
    ^(global|local) Match either the word global or the word local at the beginning of the string
    ([a-zA-Z_][a-zA-Z-0-9_]*) Match any sequence of characters that are letters, numeric, or an underscore that doesn't start with a character that's numeric
    (as) Match the word as
    (boolean|decimal|number|string)$ Match the word: Boolean, decimal, number, or string at the end of the sentence
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width