S
S
Sant Ferrand2021-07-17 10:34:20
Python
Sant Ferrand, 2021-07-17 10:34:20

Tab algorithm from python?

I'm writing my own lexical analyzer and I need a tab detection algorithm like from python.
In python.

I already made 3 prototypes:
Just with tab count:

def GetTabs(code):
  index = 0

  for char in code:
    if char == '\t': index+=1
    else: return index

Counting tabs and spaces
def GetTabs(code):
  index = 0
  SpaceCount = 1

  for char in code:
    if not char.isspace(): return index

    if char == '\t': index+=1
    else:
      if SpaceCount == 4:
        index+=1
        SpaceCount = 1

      elif char == " ":
        SpaceCount+=1

And using .split()

But all 3 prototypes can be broken by playing with tabs and spaces.
I'm wondering what such an algorithm would look like in python itself

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GavriKos, 2021-07-17
@GavriKos

Python can also be broken if you mix tabs and spaces.

A
Alan Gibizov, 2021-07-17
@phaggi

No, not the tabulation algorithm from Python.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question